1.Express 简介
Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具。
使用 Express 可以快速地搭建一个完整功能的网站。
Express 框架核心特性:
可以设置中间件来响应 HTTP 请求。
定义了路由表用于执行不同的 HTTP 请求动作。
可以通过向模板传递参数来动态渲染 HTML 页面。
2.安装 Express
npm install express --save
以上命令会将 Express 框架安装在当前目录的 node_modules 目录中, node_modules 目录下会自动创建 express 目录。以下几个重要的模块是需要与 express 框架一起安装的:
body-parser - node.js 中间件,用于处理 JSON, Raw, Text 和 URL 编码的数据。
cookie-parser - 这就是一个解析Cookie的工具。通过req.cookies可以取到传过来的cookie,并把它们转成对象。
multer - node.js 中间件,用于处理 enctype=“multipart/form-data”(设置表单的MIME编码)的表单数据。
npm install body-parser --save
npm install cookie-parser --save
npm install multer --save
3.请求和响应
Express 应用使用回调函数的参数: request 和 response 对象来处理请求和响应的数据。
//app对象通常表示Express应用程序
app.get('/',(req,res)=>{
// res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
// res.end('get请求');
res.send('get请求');
});
4.创建服务器:
//引入express
const express = require('express');
const app = express();
//'/'表示对根路径的请求
//get请求
app.get('/',(req,res)=>{
// res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
// res.end('get请求');
res.send('get请求');
});
//post请求
app.post('/',(req,res)=>{
res.send('post请求');
});
app.listen(3007,()=>{
console.log('running....')
});
5.创建静态资源:
// 托管静态文件:
// 可以指定虚拟目录
// 可以指定多个目录作为静态资源目录
const express = require('express');
const app = express();
//实现静态资源服务
//可以访问public目录下的所有文件,例如http://localhost:3007/js/app.js
//use方法的第一个参数可以指定一个虚拟路径
//必须加上这个虚拟目录才能访问,如:http://localhost:3007/abc/pac.html
app.use('/abc',express.static('../public'),(req,res)=>{
res.send('pac.html');
});
//如果要使用多个静态资源目录,请多次调用 express.static 中间件函数
app.use(express.static('../www'));
app.listen(3007,()=>{
console.log('running...');
});
6.基本路由操作:
路由:根据请求路径和请求方式进行路径分发处理
- http的常用请求方式:
- post 添加
- get 查询
- put 更新
- delete 删除
//引入express
const express = require('express');
const app = express();
//基本的路由处理
//表单只有两种提交方式:get和post
//'/'表示对根路径的请求
//查询
app.get('/',(req,res) => {
res.send('查询');
});
//添加
app.post('/',(req,res) => {
res.send('添加');
});
//更新
app.put('/',(req,res) => {
res.send('更新');
});
//删除
app.delete('/',(req,res) => {
res.send('删除');
});
//直接使用use方法可以处理所有的路由请求
app.use((req,res)=>{
res.send('use方法');
});
app.listen(3007,()=>{
console.log('running...');
});
7. 使用中间件:
(1)应用级中间件
next函数主要负责将控制权交给下一个中间件,如果当前中间件没有终结请求,并且next没有被调用,那么请求将被挂起,后边定义的中间件将得不到被执行的机会。
// 中间件,就是处理过程中的一个环节(本质上就是一个函数)
//应用级别的中间件
const express = require('express');
const app = express();
let total = 0;
//next可以将中间件彼此之间串联起来
app.use('/',(req,res,next) => {
//next方法主要是用来确保所有注册的中间件被一个接一个的执行。
next();
});
app.use('/',(req,res,next) => {
next();
});
//最后一个中间件不需要往下传递
app.use('/',(req,res) => {
//记录访问次数
total++;
console.log(total);
});
app.listen(3007,()=>{
console.log('running...');
});
(2)使用中间件:
路由级的中间件
const express = require('express');
const app = express();
app.post('/',(req,res,next) => {
console.log(1);
//如果不使用next,则后面的函数都无法调用,处于阻塞状态
//如果有next()不带参数,只打印1 2;next()带参数表示跳转到下一个路由,只打印:1 3
next('route')
},(req,res) => {
console.log(2);
res.send('中间件的挂载方式和执行流程');
});
app.post('/',(req,res) => {
console.log(3);
res.send('hello');
});
app.listen(3007,()=>{
console.log('running...');
});