php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 605|回复: 0

Node.js---中间件

[复制链接]

2670

主题

2677

帖子

9495

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6703
贡献
0
注册时间
2021-4-14
最后登录
2024-5-15
在线时间
674 小时
QQ
发表于 2022-1-13 09:54:02 | 显示全部楼层 |阅读模式
一、中间件概念
中间件(Middleware ),特指业务流程的中间处理环节。

1、Express 中间件的调用流程
        当一个请求到达 Express 的服务器之后,可以连续调用多个中间件,从而对这次请求进行预处理。如下所示:
image.png
2、Express 中间件的格式
        Express 的中间件,本质上就是一个 function 处理函数,Express 中间件的格式如下:
[mw_shl_code=applescript,true]var express = require('express');
var app = express();
app.get('/',(res,req,next)=>{
        next();
        });
app.listen(3000);
[/mw_shl_code]
注意:中间件函数的形参列表中,必须包含 next 参数。

3、next 函数的作用
        next 函数是实现多个中间件连续调用的关键,它表示把流转关系转交给下一个中间件或路由。
image.png
二、Express 中间件的使用
1、定义中间件函数
如下所示:
[mw_shl_code=applescript,true]const express = require('express');
const app = express();
//定义一个最简单的中间件函数
const mw = (req,res,next)=>{
    console.log('这是最简单的中间件函数')
    //把流转关系,转交给下一个中间件或路由
    next();
}
app.listen(80,()=>{
    console.log('http://127.0.0.1')
})
[/mw_shl_code]
2、全局生效的中间件
        客户端发起的任何请求,到达服务器之后,都会触发的中间件,叫做全局生效的中间件。
        通过调用 app.use(中间件函数),即可定义一个全局生效的中间件,示例代码如下:
[mw_shl_code=applescript,true]const mw = function (req,res,next){
    console.log('这是最简单的中间件函数')
    //把流转关系,转交给下一个中间件或路由
    next()
}
//将mw注册为全局生效的中间件
app.use(mw)
[/mw_shl_code]
如下:

[mw_shl_code=applescript,true]const express = require('express');
const app = express();
//定义一个最简单的中间件函数
const mw = function (req,res,next){
    console.log('这是最简单的中间件函数')
    //把流转关系,转交给下一个中间件或路由
    next()
}
//将mw注册为全局生效的中间件
app.use(mw)
app.get('/',(req,res)=>{
    res.send('Home page.')
})
app.post('/',(res,req)=>{
    res.send('user page.')
})
app.listen(80,()=>{
    console.log('http://127.0.0.1')
})
[/mw_shl_code]
先在终端运行:
image.png
打开postman,进行测试:
get测试:
image.png
发现终端输出:
image.png
这是因为当我们向服务器响应请求时,服务器会先将请求交中间件处理,当中间件处理完成后,会调用一次next()函数,将流转关系,转交给下一个中间件或路由。
        post测试方法上同,不在赘述。

3、定义全局中间件的简化形式
如下所示:
[mw_shl_code=applescript,true]app.use(function (req,res,next){
    console.log('这是最简单的中间件函数')
    //把流转关系,转交给下一个中间件或路由
    next()
})
[/mw_shl_code]
进行测试,发现也可以成功调用。

4、中间件的作用
        多个中间件之间,共享同一份 req 和 res。基于这样的特性,我们可以在上游的中间件中,统一为 req 或 res 对象添加自定义的属性或方法,供下游的中间件或路由进行使用。
        例如:请求到达服务器之后,得到每个路由此次请求的时间。代码如下:
[mw_shl_code=applescript,true]const express = require('express')
const app = express();
app.use((req,res,next)=>{
    //获取到请求到服务器的时间
    const time = Date.now()
    //为req对象挂载自定义属性,从而把时间共享给后面所有的路由
    req.startTime = time;
    next();
})
app.get('/user1',(req,res)=>{
    res.send('Home page'+req.startTime)
})
app.get('/user2',(req,res)=>{
    res.send('User page'+req.startTime)
})
app.listen(80,()=>{
    console.log('http://127.0.0.1')
})
[/mw_shl_code]
在终端运行:
image.png
然后打开postman,进行测试:
image.png
image.png
测试成功。

5、定义多个全局中间件
        可以使用 app.use() 连续定义多个全局中间件。客户端请求到达服务器之后,会按照中间件定义的先后顺序依次进行调用,示例代码如下:
[mw_shl_code=applescript,true]const express = require('express');
const app = express();
//定义第一个中间件
app.use((req,res,next)=>{
    console.log('这是第一个中间件')
    next();
})
//定义第二个中间件
app.use((req,res,next)=>{
    console.log('这是第二个中间件')
    next()
})
//定义路由
app.get('/user',(req,res)=>{
    res.send('User page.')
})
app.listen(80,()=>{
    console.log('http://127.0.0.1')
})
[/mw_shl_code]
在终端运行:
image.png
打开postman进行测试:
image.png
在打开终端,可以看到终端输出:
image.png
测试成功。

6、局部生效的中间件
        不使用 app.use() 定义的中间件,叫做局部生效的中间件,示例代码如下:
[mw_shl_code=applescript,true]const express = require('express')
const app = express();
//定义中间件函数
const mw1 = (req,res,next)=>{
    console.log('调用了局部生效的中间件')
    next()
}
//创建路由
app.get('/',mw1,(req,res)=>{
    res.send('Home page')
})
app.get('/user',(req,res)=>{
    res.send('User page')
})
app.listen(80,()=>{
    console.log('http://127.0.0.1')
})
[/mw_shl_code]
在终端运行:
image.png
打开postman进行测试:
image.png
在回到终端,发现输出:
image.png
再去postman调用第二个路由,如下:
image.png
终端并没有输出中间件函数的内容,说明该中间件是局部生效的。测试成功。

7、定义多个局部中间件
可以在路由中,通过如下两种等价的方式,使用多个局部中间件:
[mw_shl_code=applescript,true]//写法1
app.get('/',mv1,mv2,(req,res)=>{res.send('Home page')}
//写法2
app.get('/',[mv1,mv2],(req,res)=>{res.send('Home page')}
[/mw_shl_code]
如下:

[mw_shl_code=applescript,true]const express = require('express');
const app = express();
//定义中间件函数
const mv1 = (res,req,next)=>{
    console.log('调用了第一个局部生效的中间件')
    next()
}
const mv2 = (req,res,next)=>{
    console.log('调用了第二个局部生效的中间件')
    next()
}
//定义路由
app.get('/',mv1,mv2,(req,res)=>{
    res.send('Home page')
})
app.get('/user',(req,res)=>{
    res.send('User page')
})
app.listen(80,()=>{
    console.log('http://127.0.0.1')
})
[/mw_shl_code]
在终端启动服务器:
image.png
打开postman进行测试,先开启第一个路由:

image.png
image.png
再开启第二个路由:
image.png
  终端无其他输出,说明该中间件是局部生效的。测试成功。
需要注意的是:

一定要在路由之前注册中间件
客户端发送过来的请求,可以连续调用多个中间件进行处理
执行完中间件的业务代码之后,不要忘记调用 next() 函数
为了防止代码逻辑混乱,调用 next() 函数后不要再写额外的代码
连续调用多个中间件时,多个中间件之间,共享 req 和 res 对象





上一篇:极客日报:马化腾:腾讯只是一家普通公司,随时都可以...
下一篇:Node.js---中间件分类
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )

GMT+8, 2024-5-17 10:04 , Processed in 0.159907 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表