中间件
概念
代码举例
var http = require('http')
//这些就是中间件
var cookie = require('./middlewares/cookie')
var postBody = require('./middlewares/post-body')
var query = require('./middlewares/query')
var session = require('./middlewares/session')
var server = http.createServer(function (req, res) {
// 解析表单 get 请求体
// 解析表单 post 请求体
// 解析 Cookie
// 处理 Session
// 使用模板引擎
//使用插件可以得到以下
// console.log(req.query)
// console.log(req.body)
// console.log(req.cookies)
// console.log(req.session)
// 解析请求地址中的 get 参数
// var urlObj = url.parse(req.url, true)
// req.query = urlObj.query
query(req, res)
// 解析请求地址中的 post 参数
// req.body = {
// foo: 'bar'
//}
postBody(req, res)
// 解析 Cookie
// req.cookies = {
// isLogin: true
// }
cookie(req, res)
// 配置 Session
// req.session = {}
session(req, res)
// 配置模板引擎
res.render = function () {
}
//将上述的操作封装成小模块文件,这些就是个中间件,并在前面申明使用
if (req.url === 'xxx') {
// 处理
// query、body、cookies、session、render API 成员
} else if (url === 'xx') {
// 处理
}
// 上面的过程都是了为了在后面做具体业务操作处理的时候更方便
})
server.listen(3000, function () {
console.log('3000. running...')
})
封装成的中间件
中间件说白了就是个方法,每个方法有特殊的含义。
express中的中间件
- 先安装express
npm i express
- d代码举例
var express = require('express')
var app = express()
// app.use(function (req, res, next) {
// console.log('1')
// next()
// })
// app.use(function (req, res, next) {
// console.log('2')
// next()
// })
// app.use(function (req, res, next) {
// console.log('3')
// res.send('333 end.')
// })
// app.use(function (req, res, next) {
// console.log(1)
// next()
// })
// app.use('/b', function (req, res, next) {
// console.log('b')
// })
// // 以 /xxx 开头的路径中间件
// app.use('/a', function (req, res, next) {
// console.log('a')
// next()
// })
// app.use(function (req, res, next) {
// console.log('2')
// next()
// })
// app.use('/a', function (req, res, next) {
// console.log('a 2')
// })
// 除了以上中间件之外,还有一种最常用的
// 严格匹配请求方法和请求路径的中间件
// app.get
// app.post
app.use(function (req, res, next) {
console.log(1)
next()
})
app.get('/abc', function (req, res, next) {
console.log('abc')
next()
})
app.get('/', function (req, res, next) {
console.log('/')
next()
})
app.use(function (req, res, next) {
console.log('haha')
next()
})
app.get('/abc', function (req, res, next) {
console.log('abc 2')
})
app.use(function (req, res, next) {
console.log(2)
next()
})
app.get('/a', function (req, res, next) {
console.log('/a')
})
app.get('/', function (req, res, next) {
console.log('/ 2')
})
// 如果没有能匹配的中间件,则 Express 会默认输出:Cannot GET 路径
app.listen(3000, function () {
console.log('app is running at port 3000.')
})
中间件:处理请求的,本质就是个函数
在 Express 中,对中间件有几种分类
不关心请求路径和请求方法的中间件
也就是说任何请求都会进入这个中间件
app.use(function (req, res) {
console.log(‘请求进来了’)
})
不关心请求路径:在浏览器上输入任何地址,它会在控制台中报出信息
不关心请求方法:get,post等等都能进来
中间件本身是一个方法,该方法接收三个参数:
Request 请求对象
Response 响应对象
next 下一个中间件
app.use(function (req, res, next) {
console.log('1')
next()
})
app.use(function (req, res, next) {
console.log('2')
})
如果没有next的话,控制台显示的信息与这个代码顺序有关,有了next就是调用下面的代码,可以显示出显示出1,2
当一个请求进入一个中间件之后,如果不调用 next 则会停留在当前中间件
所以 next 是一个方法,用来调用下一个中间件的。
next会向下进行匹配,只有匹配成功之后才会成立。
app.use(function (req, res, next) {
console.log('1')
next()
})
app.use(function (req, res, next) {
console.log('2')
next()
})
app.use(function (req, res, next) {
console.log('3')
res.send('333 end.')
})
进入中间件,并使用next方法进去下一个中间件
显示1,2,3
调用 next 方法也是要匹配的(不是调用紧挨着的那个)
// 以 /xxx 开头的路径中间件
app.use('/b', function (req, res, next) {
console.log('b')
})
app.use('/a', function (req, res, next) {
console.log('a')
})
要进行匹配,然后根据路径是谁开头来返回确定的信息。
app.use(function (req, res, next) {
console.log('2')
next()
})
app.use('/a', function (req, res, next) {
console.log('a 2')
})
当请求进来,会从第一个中间件开始进行匹配
- 如果匹配,则进来
- 如果请求进入中间件之后,没有调用 next 则代码会停在当前中间件
- 如果调用了 next 则继续向后找到第一个匹配的中间件
- 如果不匹配,则继续判断匹配下一个中间件
除了以上中间件之外,还有一种最常用的
严格匹配请求方法和请求路径的中间件
app.get
app.post
- 应用程序级别中间件
万能匹配(不关心任何请求路径和请求方法)
app.use(function (req, res, next) {
console.log('1')
next()
})
app.use(function (req, res, next) {
console.log('2')
next()
})
app.use(function (req, res, next) {
console.log('3')
res.send('333 end.')
})
- 路由级别中间件
get,post,put,delete
app.get('/abc', function (req, res, next) {
console.log('abc')
next()
})
app.get('/', function (req, res, next) {
console.log('/')
next()
})
app.use(function (req, res, next) {
console.log('haha')
next()
})
- 错误处理中间件
app.use(function (err, req, res, next){
console.error(err, stack)
res.status(500).send('Something broke!')
})
- 内置中间件
express.static | serves static such as HTML files,images… |
---|---|
express.json | parses incoming requests with JSON payloads |
– | – |
express.urlencoded | parses incoming requests with URL |
- 第三方中间件
· body-parser
· compression
· cookie-parser
· morgan
· response-time
· serve-static
· session