Express 的中间件

使用 express 处理请求的时候,一般都在后面添加个回调,每个回调都包含req、res和next。而这一个个回调函数就是我们要学的中间件。

利用中间件我们可以做什么事情呢?

  1. 执行这任意代码。
  2. 更改请求对象和响应对象。
  3. 结束请求-响应周期。
  4. 调用堆栈中的下一个中间件函数。

如果当前中间件函数没有结束请求-响应周期,它必须调用 next() 将控制传递给下一个中间件函数。否则,请求将被挂起。

下图显示了中间件函数调用的元素:

从Express 5开始,返回 Promise 的中间件函数将在 reject 或抛出错误时调用 next(value)。next 将被调用,并返回被拒绝的值或抛出的错误。

关于中间的分类有哪些呢?

  1. 应用层的中间件:其实我们可以写包含req、res和next的一个函数,然后通过app.use注册一下,这样它就算一个应用层的中间件了。在请求处理的过程中,它就会被执行。
  2. 路由器级中间件:Express 的路由
  3. 错误处理中间件:Express 的错误处理
  4. 内置中间件:例如express.json()、express.urlencoded()等
  5. 第三方中间件:社区下载的中间件

不管分多少类,本质上都是包含req、res和next的回调。

关于中间件的调用,它永远先调用第一个匹配的中间件,调用完成后,看有没有next()去连接下一个中间件,没有的话就挂起,有就一直执行下去。

连续使用中间件的时候,通常把响应放在最后一个中间件。

javascript 复制代码
const express = require('express');
const app = express();
const port = 3000;

const middleware1 = (req, res, next) => {
    console.log('middleware1')
    next()
}
const middleware2 = (req, res, next) => {
    console.log('middleware2')
    next()
}
const middleware3 = (req, res, next) => {
    console.log('middleware3')
    next()
}

app.use(middleware1)
app.use(middleware2)
app.use(middleware3)

app.get('/', function (req, res, next) {
    console.log('express')
    res.json('express')
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

// 打印结果
middleware1
middleware2
middleware3
express

如果我想往请求里添加参数,怎么做?

javascript 复制代码
const express = require('express');
const app = express();
const port = 3000;

const middleware = (req, res, next) => {
    req.name = 'jiawei'
    req.age = 18
    next()
}

app.use(middleware)

app.get('/info', function (req, res, next) {
    console.log('req.name:', req.name)
    console.log('req.age:', req.age)
    res.json('express')
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

Postman请求 localhost:3000/info,打印结果

javascript 复制代码
req.name: jiawei
req.age: 18

如果是只想往特定的请求里添加数据呢?

javascript 复制代码
const express = require('express');
const app = express();
const port = 3000;

const middleware = (req, res, next) => {
    req.name = 'jiawei'
    req.age = 18
    next()
}

// app.use(middleware)

app.get('/info', function (req, res, next) {
    console.log('req.name:', req.name)
    console.log('req.age:', req.age)
    res.json('express')
})

app.get('/user', middleware, function (req, res, next) {
    console.log('req.name:', req.name)
    console.log('req.age:', req.age)
    res.json('express')
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

这时,Postman 请求 localhost:3000/info,结果如下

javascript 复制代码
req.name: undefined
req.age: undefined

而 Postman 请求 localhost:3000/user,结果如下

javascript 复制代码
req.name: jiawei
req.age: 18
相关推荐
莹雨潇潇32 分钟前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
Jiaberrr41 分钟前
Element UI教程:如何将Radio单选框的圆框改为方框
前端·javascript·vue.js·ui·elementui
XKSYA(小巢校长)1 小时前
NatGo我的世界联机篇
开发语言·php
Cons.W1 小时前
Codeforces Round 975 (Div. 1) C. Tree Pruning
c语言·开发语言·剪枝
憧憬成为原神糕手1 小时前
c++_ 多态
开发语言·c++
VBA63371 小时前
VBA信息获取与处理第三个专题第三节:工作薄在空闲后自动关闭
开发语言
Tiffany_Ho1 小时前
【TypeScript】知识点梳理(三)
前端·typescript
wjs20242 小时前
XSLT 实例:掌握 XML 转换的艺术
开发语言
萧鼎2 小时前
Python第三方库选择与使用陷阱避免
开发语言·python
安冬的码畜日常2 小时前
【D3.js in Action 3 精译_029】3.5 给 D3 条形图加注图表标签(上)
开发语言·前端·javascript·信息可视化·数据可视化·d3.js