Node.js 入门 —— 中间件

文章目录

    • 1 前言
    • 2 中间件的概念
      • 2.1 什么是中间件
      • 2.2 中间件的调用流程
      • 2.3 Express 中间件的格式
    • 3 express 中间件
      • 3.1 定义中间件函数
      • 3.2 全局生效的中间件
      • 3.3 局部生效的中间件
      • 3.4 中间件使用注意事项
      • 3.5 中间件的分类
    • 4 自定义中间件

1 前言

本文内容来自bilibili 黑马程序员

2 中间件的概念

2.1 什么是中间件

中间件(middleware)特指业务流程的中间处理环节

2.2 中间件的调用流程

当一个请求到达 Express 的服务器之后,可以连续调用多个中间件,从而对这次请求进行预处理

2.3 Express 中间件的格式

本质上是一个 function 处理函数
中间件函数的形参列表中,必须包含 next 参数,而路由处理函数只包含 req 和 res
next 函数是实现多个中间件连续调用的关键,它表示将流转关系流转给下一个中间件或者路由

3 express 中间件

3.1 定义中间件函数

// 定义一个最简单的中间件函数
const mw = (req, res, next) => {
  console.log('这是最简单的中间件函数');
  // 把流转关系,转交给下一个中间件或者路由
  next()
}

3.2 全局生效的中间件

客户端发起的任何请求,到达服务器之后都会触发的中间件,叫做全局生效中间件
通过调用 app.use(中间件函数),即可定义一个全局生效的中间件

app.use(mw)

3.3 局部生效的中间件

不使用 app.use() 定义的中间件,叫做局部生效的中间件

app.get('/user', mw, (req, res) => {})

3.4 中间件使用注意事项

  1. 除错误级别中间件外,其他类型中间件一定要在路由之前注册
  2. 客户端发送过来的请求,可以连续调用多个中间件进行处理
  3. 执行完中间件的业务代码之后,不要忘记调用 next() 函数
  4. 为了防止代码逻辑混乱,调用 next() 函数后不要再写额外的代码
  5. 连续调用多个中间件时,多个中问件之间,共享 req 和 res 对象

3.5 中间件的分类

为了方便大家理解和记忆中间件的使用,Express 官方把常见的中间件用法,分成了5大类,分别是:

  1. 应用级别的中间件
    通过 app.use() 、 app.get() 、 app.post() ,绑定到 app 实例上的中间件
// 应用级别的中间件(全局中间件)
app.use((req, res, next) => {
  next()
})
// 应用级别的中间件(局部中间件)
app.get('/', mw, (req, res) => {
  res.send('Hone Npage.')
})
  1. 路由级别的中间件
    绑定到 express.Router() 上的中间件
const app = express()
const router = express.Router()
// 路由级别的中间件
router.use(function (req, res, next) {
  console.log( "Time:" Date.now())
  next()
})
app.use(router)
  1. 错误级别的中间件
    作用:专门用来捕获整个项目中发生的异常错误,从而防止项目异常崩溃的问题
    格式:错误级别中间件的 function 处理函数中,必须有 4 个形参,形参顺序从前到后,分别是 (err, req, res, next)
app.get('/', (req, res) => {
  throw new Error('服务器发生错误')
  res.send('Home Page')
})
app.use((err, req, res, next) => {
  console.log('发生了错误!', err.message);
  res.send(`发生了错误!${err.message}`)
})
  1. Express 内置的中间件
  • express.static() :快速托管静态资源
  • express.json() :解析 JSON 格式的请求数据
  • express.urlencoded() :解析 URL-encoded 格式的请求数据
  1. 第三方的中间件
    非 express 官方提供的,而是由第三方开发出来的中间件

4 自定义中间件

自己手动模拟一个类似于 express.urlencoded 这样的中间件,来解析 POST 提交到服务器的表单数据

实现步骤:

  1. 定义中间件
  2. 监听 req 的 data 事件
  3. 监听 req 的 end 事件
  4. 使用 querystring 模块解析请求体数据
  5. 将解析出来的数据对象挂载为 req.body
  6. 将自定义中间件封装成模块

custom-bodyparser.js

const qs = require('querystring') 

const bodyParser = (req, res, next) => {
  let str = ''
  req.on('data', (chunk) => {
    str += chunk
  })
  req.on('end', () => {
    req.body = qs.parse(str)
    next()
  })
}

module.exports = bodyParser

index.js

const express = require('express')
const bodyParser = require('./custom-bodyparser') 
const app = express()

app.use(bodyParser)

app.post('/user', (req, res) => {
  res.send(req.body)
})

app.listen(80, () => {
  console.log('http://127.0.0.1');
})

你可能感兴趣的:(Node,中间件,node.js,java)