You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constexpress=require('express')constbodyParser=require('body-parser')constapp=express()constport=3000app.use(express.static('public'));// 创建 application/x-www-form-urlencoded 编码解析varurlencodedParser=bodyParser.urlencoded({extended: false})// next 函数表示进入下一个中间件app.get('/index',(req,res,next)=>{req.data=123next()},function(req,res,next){console.log('通过中间件取到的值:',req.data)res.send('end')})app.listen(port,()=>console.log(`Example app listening on port ${port}!`))
varapp=express()// 使用路由中间件varrouter=express.Router()// a middleware function with no mount path. This code is executed for every request to the routerrouter.use(function(req,res,next){console.log('Time:',Date.now())next()})// a middleware sub-stack shows request info for any type of HTTP request to the /user/:id pathrouter.use('/user/:id',function(req,res,next){console.log('Request URL:',req.originalUrl)next()},function(req,res,next){console.log('Request Type:',req.method)next()})// a middleware sub-stack that handles GET requests to the /user/:id pathrouter.get('/user/:id',function(req,res,next){// if the user ID is 0, skip to the next routerif(req.params.id==='0')next('route')// otherwise pass control to the next middleware function in this stackelsenext()},function(req,res,next){// render a regular pageres.render('regular')})// handler for the /user/:id path, which renders a special pagerouter.get('/user/:id',function(req,res,next){console.log(req.params.id)res.render('special')})// mount the router on the appapp.use('/',router)
错误捕获中间件
使用方式:app.use(function (err, req,res, next) {})
demo:
constexpress=require('express')constbodyParser=require('body-parser')constapp=express()constport=3000app.use(express.static('public'));// 创建 application/x-www-form-urlencoded 编码解析varurlencodedParser=bodyParser.urlencoded({extended: false})app.get('/index',(req,res,next)=>{connsole.log(11)// 这里写错一个单词})// 使用错误中间件捕获异常app.use(function(err,req,res,next){console.error(err.stack)res.status(500).send('Something broke!')})app.listen(port,()=>console.log(`Example app listening on port ${port}!`))
当访问:localhost:3000/index 时候,返回:Something broke!
注意:错误中间件的位置可以放在所有请求前面,也可以放在后面,例如:
请求处理前格式判断是否错误等
请求处理中可能遇到报错,错误捕获放在最后兜住前面的错误
cookie-parser 中间件
安装:
npm install cookie-parser
使用:
constexpress=require('express')constbodyParser=require('body-parser')constcookieParser=require('cookie-parser')constapp=express()constport=3000app.use(express.static('public'));// 使用 cookie-parser 中间件app.use(cookieParser())// 创建 application/x-www-form-urlencoded 编码解析varurlencodedParser=bodyParser.urlencoded({extended: false})app.get('/index',(req,res,next)=>{// 使用 cookie-parser 中间件后,这里才能拿到 cookiesres.send(req.cookies)})app.listen(port,()=>console.log(`Example app listening on port ${port}!`))
在浏览器手动写入 cookie 作为测试
输入地址访问:localhost:3000/index 拿到 cookie
The text was updated successfully, but these errors were encountered:
Express 中间件
参考文档:使用中间件-express
next函数
作用:暂停,然后传递至下一个中间件
使用前面的例子:
路由中间件
类似一个mini 的 app,使用方式相对简单
使用方式:
具体代码:
错误捕获中间件
使用方式:
app.use(function (err, req,res, next) {})
demo:
当访问:
localhost:3000/index
时候,返回:Something broke!
注意:错误中间件的位置可以放在所有请求前面,也可以放在后面,例如:
cookie-parser 中间件
安装:
使用:
在浏览器手动写入 cookie 作为测试
输入地址访问:
localhost:3000/index
拿到 cookieThe text was updated successfully, but these errors were encountered: