当前位置:网站首页>1-17 express Middleware
1-17 express Middleware
2022-06-30 21:32:00 【Endless pie】
express middleware
middleware , It refers to the intermediate process of business process
express Middleware concept
When a request arrives express After the server of , Multiple middleware can be called continuously , To preprocess this request
express Middleware format
express Middleware , It's essentially one function Processing function
Be careful : In the formal parameter list of middleware functions , Must contain next Parameters . The routing processing function only contains req and res.
next Function function
next The key to realize the continuous call of multiple Middleware in a functional way , It means transferring the flow relationship to the next middleware or routing .
Define middleware functions
const ww = function(req,res,next){
console.log(' Middleware function ')
next()
}
Globally effective middleware
app.use((req,res,next)=>{
const time = Date.now();
req.time = time
next()
})
// Global middleware can define multiple , It's done in order
Partially effective middleware
// Don't use app.use() Defined middleware , Middleware called local validation
const mw = ((req,res,next)=>{
console.log(' Local 1 middleware ')
next()
})
const ms = ((req,res,next)=>{
console.log(' Local 2 middleware ')
next()
})
// You can also define multiple local middleware
//[mw,ms] It's OK to put it in an array
app.get('/',mw,ms,(req,res)=>{
res.send('666')
})
app.post('/',(req,res)=>{
res.send('88')
})
Middleware considerations :
- Be sure to register middleware before routing
- The request sent by the client , Multiple middleware processes can be called continuously
- After executing the business code of middleware , Don't forget to call next() function
- In order to prevent code logic confusion , call next() Don't write extra code after the function
- When calling multiple Middleware in succession , Between multiple middleware , share req and res object
Classification of middleware
1 Application level middleware
adopt app,use() or app.get() or app.post(), Bound to the app Middleware on strength , It's called application level middleware
app.use((req,res,next)=>{
next()
})
app.get('/',nw1,(req,res)=>{
res.send('hellp')
})
2. Routing level middleware
The routing level middleware is bound to express.Router() Middleware on instance , It's called routing level middleware .
var app = express()
var router = express.Router()
// Routing level middleware
router.use(function(){
next()
})
2. Error level middleware
The role of error level middleware : It's designed to catch unexpected errors in the entire project 、 So as to prevent the abnormal collapse of the project .
Format : Error level middleware function In the processing function , There has to be 4 Parameters , The order of formal parameters is from front to back , Namely (err,req,res,next)
const express = require('express')
const app = express()
app.get('/',(req,res)=>{
throw new Error(' An error occurred inside the server ')
res.send('home page')
})
// Middleware with error level , Replenishment error , Prevent collapse
app.use((err,req,res,next)=>{
console.log(' Something went wrong '+err.message)
res.send(`Error${err.message}`)
})
app.listen(80,()=>{
console.log('server and at ')
})
Be careful : Error level middleware must be registered after all routes .
express Built-in middleware
- express.static() Fast hosting of static resources .
- express.json() analysis JSON Request body data in format ( Compatibility , Only in 4.16.0+ Version available )
- express.urlencoded() analysis URL-encoded Request body data in format ( Compatibility , Only in 4.16.0+ Version available )
app.use(express.static('./file'))
app.user(express.json())
app.user(express.urlencoded({extended:false}))
Third party middleware
Not express The official built-in , It's middleware developed by a third party . In the project , You can download and configure the third-party middle price on demand , So as to improve the development efficiency of the project .
边栏推荐
- twelve thousand three hundred and forty-five
- A group of K inverted linked lists
- 激发新动能 多地发力数字经济
- 用yml文件进行conda迁移环境时的报错小结
- 攻防演练中的防泄露全家福
- Electronic scheme development - Intelligent rope skipping scheme
- vim 常用快捷键
- Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?
- Neurotransmetteurs excitateurs - glutamate et santé cérébrale
- Export El table as is to excel table
猜你喜欢
随机推荐
Arcmap|assign values to different categories of IDS with the field calculator
Zaah Sultan looks at the old driver
Spatiotemporal data mining: an overview
1-1 数据库的基本概念
Open source internship experience sharing: openeuler software package reinforcement test
Jupyterbook clear console output
Adobe Photoshop (PS) - script development - remove file bloated script
Digital currency: far-reaching innovation
的撒啊苏丹看老司机
介绍一款|用于多组学整合和网络可视化分析的在线平台
Radar data processing technology
What does grade evaluation mean? What is included in the workflow?
Export El table as is to excel table
Who are you and I
CA I ah, several times Oh, ah, a sentence IU home Oh
1-12 初步认识Express
【无标题】第一次参加csdn活动
Oracle 数据库表结构 Excel 导出
Neurotransmetteurs excitateurs - glutamate et santé cérébrale
你我他是谁



![[untitled]](/img/42/47a8c8faaed33a1d9e864cb2ef7b72.png)


