当前位置:网站首页>1-17 express中间件
1-17 express中间件
2022-06-30 21:26:00 【画不完的饼】
express中间件
中间件,特指业务流程的中间处理环节
express中间件的概念
当一个请求到达express的服务器之后,可以连续调用多个中间件,从而对这次请求进行预处理
express中间件的格式
express的中间件,本质上就是一个function处理函数
注意:中间件函数的形参列表中,必须包含next参数。而路由处理函数中只包含req和res.
next函数的作用
next函数式实现多个中间件连续调用的关键,它表示把流转关系转交给下一个中间件或路由。
定义中间件函数
const ww = function(req,res,next){
console.log('中间件函数')
next()
}
全局生效的中间件
app.use((req,res,next)=>{
const time = Date.now();
req.time = time
next()
})
//全局中间件可以定义多个,是按顺序执行的
局部生效的中间件
//不使用app.use()定义的中间件,叫做局部生效的中间件
const mw = ((req,res,next)=>{
console.log('局部1中间件')
next()
})
const ms = ((req,res,next)=>{
console.log('局部2中间件')
next()
})
//还可以定义多个局部中间件
//[mw,ms] 放在数组里面也是可以的
app.get('/',mw,ms,(req,res)=>{
res.send('666')
})
app.post('/',(req,res)=>{
res.send('88')
})
中间件注意点:
- 一定要在路由之前注册中间件
- 客户端发送过来的请求,可以连续调用多个中间件处理
- 执行完中间件的业务代码之后,不要忘记调用next()函数
- 为了防止代码逻辑混乱,调用next()函数后不要在写额外的代码
- 连续调用多个中间件时,多个中间件之间,共享req和res对象
中间件的分类
1应用级别的中间件
通过app,use()或app.get()或app.post(),绑定到app实力上的中间件,叫做应用级别的中间件
app.use((req,res,next)=>{
next()
})
app.get('/',nw1,(req,res)=>{
res.send('hellp')
})
2.路由级别的中间件
路由级别的中间件绑定到express.Router()实例上的中间件,叫做路由级别的中间件。
var app = express()
var router = express.Router()
//路由级别的中间件
router.use(function(){
next()
})
2.错误级别的中间件
错误级别中间件作用:专门用来捕获整个项目中发生的异常错误、从而防止项目异常崩溃的问题。
格式:错误级别中间件的function处理函数中,必须有4个形参,形参顺序从前到后,分别是(err,req,res,next)
const express = require('express')
const app = express()
app.get('/',(req,res)=>{
throw new Error('服务器内部发生了错误')
res.send('home page')
})
//定错误级别的中间件,补货错误,防止崩溃
app.use((err,req,res,next)=>{
console.log('发生了错误'+err.message)
res.send(`Error${err.message}`)
})
app.listen(80,()=>{
console.log('server and at ')
})
注意:错误级别的中间件必须注册在所有路由之后。
express内置的中间件
- express.static()快速托管静态资源。
- express.json()解析JSON格式的请求体数据(有兼容性,仅在4.16.0+版本可用)
- express.urlencoded()解析URL-encoded格式的请求体数据(有兼容性,仅在4.16.0+版本可用)
app.use(express.static('./file'))
app.user(express.json())
app.user(express.urlencoded({extended:false}))
第三方的中间件
非express官方内置的,而是由第三方开发出来的中间件。在项目中,可以按需下载并配置第三方中间价,从而提高项目的开发效率。
边栏推荐
- Qiao NPMS: search for NPM packages
- twelve thousand three hundred and forty-five
- 给苏丹国安德森苏丹的撒过 d s g
- 加密与解密以及OpenSSL的应用
- .netcore redis GEO类型
- Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?
- ssh-server配置文件参数PermitRootLogin介绍
- Learning summary
- 毕业五年,想当初若没有入行测试,我是否还会如这般焦虑
- Dm8: generate DM AWR Report
猜你喜欢
随机推荐
oprator-1初识oprator
布隆过滤器
CA I ah, how many times Oh, ah sentence IU home Oh 11111
《ClickHouse原理解析与应用实践》读书笔记(3)
jenkins下载插件下载不了,解决办法
凤凰架构——架构师的视角
Upgrade Kube with unknown flag: --network plugin
Three techniques for reducing debugging time of embedded software
Auto-created primary key used when not defining a primary key
给苏丹国安德森苏丹的撒过 d s g
Understanding polymorphism
攻防演练中的防泄露全家福
Who are you and I
根据肠道微生物组重新思考健康饮食
3Ds Max 精模obj模型导入ArcGIS Pro (二)要点补充
毕业五年,想当初若没有入行测试,我是否还会如这般焦虑
Fletter nested hell? No, constraintlayout to save!
Clickhouse distributed table engine
Software engineering UML drawing
Reading notes of Clickhouse principle analysis and Application Practice (3)









