中间件

中间件

概念

引用:中间件解释中间件系列

代码举例

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中的中间件

  1. 先安装express
    npm i express
  2. 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
  1. 应用程序级别中间件
    万能匹配(不关心任何请求路径和请求方法)
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.')
 })
  1. 路由级别中间件
    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()
})
  1. 错误处理中间件
app.use(function (err, req, res, next){
	console.error(err, stack)
	res.status(500).send('Something broke!')
})
  1. 内置中间件
express.staticserves static such as HTML files,images…
express.jsonparses incoming requests with JSON payloads
express.urlencodedparses incoming requests with URL
  1. 第三方中间件
    · body-parser
    · compression
    · cookie-parser
    · morgan
    · response-time
    · serve-static
    · session
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值