中间件的分类:
为了方便大家理解和记忆中间件的使用 , Express官方把常见的中间件用法 , 分成了五大类
- 应用级别的中间件
- 路由级别的中间件
- 错误级别的中间件
- Express 内置的中间件
- 第三方的中间件
举例应用之前先了解一下中间件
中间件的作用
多个中间件之间,共享同一份 req
和 res
,基于这样的特性,我们可以在上游 的中间件中,统一为 req
或 res
对象添加自定义的属性和方法,供下游的中间件或路由进行使用
中间件的五个注意事项:
-
一定要在路由之前注册中间件
-
客户端发送过来的请求,可以连续调用多个中间件进行处理
-
执行完中间件的业务代码之后,不要忘记调用
next()
函数 -
为了防止代码逻辑混乱,调用
next()
函数后不要再写额外的代码 -
连续调用多个中间件时,多个中间件之间,共享
req
和res
对象
应用级别的中间件
通过 app.use()
或 app.get()
或 app.post()
,绑定到 app
实例上的中间件,叫做应用级别的中间件
//应用级别的中间件(全局中间件)
app.use((req,res,next)=>{
next()
})
//应用级别的中间件(局部中间件)
app.get('/',mw1,(req,res)=>{
res.send('Hello Word')
})
路由级别的中间件
-
绑定到
express.Router()
实例上的中间件,叫做路由级别的中间件 -
用法上和应用级别中间件没有任何区别,只不过,应用级别中间件是绑定到
app
实例上,路由级别中间件绑定到router
实例上
const express = require('express')
const router = express.Router()
//路由级别的中间件
router.use(function(req,res,next){
consloe.log('Time',Date.now())
next()
})
app.use('/',router)
错误级别的中间件
-
错误级别中间件的作用: 专门用来捕获整个项目中发生的异常错误,从而防止项目异常崩溃的问题
-
格式:错误级别中间件的 function 处理函数中,必须有 4 个形参,形参顺序从前到后,分别是
(err, req, res, next)
-
注意: 错误级别的中间件,必须注册在所有路由之后
const express = require('express')
const app = express()
// 1. 路由
app.get('/', (req, res) => {
// 1.1 抛出一个自定义的错误
throw new Error('服务器内部发生了错误')
res.send('Home Page.')
})
// 2. 错误级别的中间件
// 注意:错误级别的中间件,必须注册在所有路由之后
app.use((err, req, res, next) => {
// 2.1 在服务器打印错误消息
console.log('发生了错误:' + err.message)
// 2.2 向客户端响应错误相关的内容
res.send(err.message)
})
app.listen(3000, () => {
console.log('running……')
})
Express内置的中间件
自 Express 4.16.0
版本开始,Express
内置了 3 个常用的中间件,极大的提高了 Express
项目的开发效率和体验
-
express.static
快速托管静态资源的内置中间件,例如: HTML 文件、图片、CSS
样式等(无兼容性) -
express.json
解析JSON
格式的请求体数据(有兼容性,仅在4.16.0+
版本中可用) -
express.urlencoded
解析URL-encoded
格式的请求体数据(有兼容性,仅在4.16.0+
版本中可用)
express.json()中间件 , 解析表单中的JSON格式数据
//导入 express
const express = require('express')
// 创建 web 服务器,命名为 app
const app = express()
// 注意:除了错误级别的中间件,其他的中间件,必须在路由之前进行配置
// 通过 express.json() 这个中间件,解析表单中的 JSON 格式的数据
app.use(express.json())
app.post('/user', (req, res) => {
// 在服务器,可以使用 req.body 这个属性,来接收客户端发送过来的请求体数据
// 默认情况下,如果不配置解析表单数据中间件,则 req.body 默认等于 undefined
console.log(req.body)
res.send('ok')
})
app.listen(3000, () => {
console.log('running……')
})
express.urlencoded解析URL-encoded格式的请求体数据
const express = require('express')
const app = express()
// 通过 express.urlencoded() 这个中间件,来解析表单中的 url-encoded 格式的数据
app.use(express.urlencoded({ extended: false }))
app.post('/book', (req, res) => {
console.log(req.body)
res.send(req.body)
})
app.listen(3000, () => {
console.log('running……')
})
第三方的中间件
-
非
Express
官方内置,而是由第三方开发出来的中间件,叫做第三方中间件。在项目中,大家可以按需下载并配置第三方中间件,从而提高项目的开发效率 -
例如:在
[email protected]
之前的版本中,经常使用body-parser
这个第三方中间件,来解析请求体数据。使用步骤如下-
运行
npm install body-parser
安装中间件 -
使用
require
导入中间件 -
调用
app.use()
注册并使用中间件
-
-
注意:
Express
内置的express.urlencoded
中间件,就是基于body-parser
这个第三方中间件进一步封装出来的const express = require('express') const app = express() // 1. 导入解析表单数据的中间件 body-parser const bodyParser = require('body-parser') // 通过 express.urlencoded() 这个中间件,来解析表单中的 url-encoded 格式的数据 // app.use(express.urlencoded({ extended: false })) // 2. 使用 app.use() 注册中间件 app.use(bodyParser.urlencoded({ extended: false })) app.post('/book', (req, res) => { console.log(req.body) res.send(req.body) }) app.listen(3000, () => { console.log('running……') })