中间件示意图.jpg

正常的响应

  • 当匹配到了请求后,交给第一个函数进行处理 ```javascript

const express = require(‘express’); const app = express();//创建一个以express应用,app实际上是一个函数,用于处理请求的函数 app.get(‘/news’, (req, res, next) => { console.log(‘handle1’); next(); }, (req, res) => { console.log(‘handle2’); next(); })

app.get(‘/news’, (req, res, next) => { console.log(‘handle3’); })

app.listen(12306, () => { console.log(‘server on 12306 has started’) })

  1. - 函数中需要手动交给后续的中间件(手动调用next)
  2. - 如果后续已经没有中间件,express会相应404
  3. - 如果中间件发生了错误,不会停止服务器,相当于调用了next(错误对象),寻找后续处理错误中间件,如果没有则响应500
  4. ```javascript
  5. const express = require('express');
  6. const app = express();//创建一个以express应用,app实际上是一个函数,用于处理请求的函数
  7. app.get('/news', (req, res, next) => {
  8. console.log('handle1');
  9. res.sendStatus('200');
  10. res.end();
  11. next();
  12. }, (req, res) => {
  13. console.log('handle2');
  14. rs.send('aa'); // 上一个中间件已经响应请求,此处不允许响应
  15. next();
  16. })
  17. app.get('/news', (req, res, next) => {
  18. console.log('handle3');
  19. })
  20. app.listen(12306, () => {
  21. console.log('server on 12306 has started')
  22. })

如果某个中间件已经处理响应,以上代码中的

  1. res.sendStatus('200');
  2. res.end();

后续的中间件就不再次响应,不能使用send向客户端发送响应,但是后续中间件依然会被调用

异常的相应

  1. const express = require('express');
  2. const app = express();//创建一个以express应用,app实际上是一个函数,用于处理请求的函数
  3. app.get('/news', (req, res, next) => {
  4. console.log('handle1');
  5. // throw new Error('abc')
  6. // 相当于throw new Error('abc')
  7. next(new Error('abcd'));
  8. }, (err,req, res,next) => { // 异常响应的第一个参数是err
  9. console.log('handle2')
  10. })
  11. app.get('/news', (req, res, next) => {
  12. console.log('handle3');
  13. })
  14. app.listen(12306, () => {
  15. console.log('server on 12306 has started')
  16. })

中间件本质

中间件本身是一个函数,用来处理请求,

  1. 自定义一个处理错误中间件 ```javascript

module.exports = (err, req, res, next) => { console.log(req.baseUrl) // 匹配路径的基地址 if (err) { const errobj = { code: 500, msg: err instanceof Error ? err.message : err } // 发生了错误 res.status(500).send(errobj) } else { next(); } }

  1. 2. 使用
  2. ```javascript
  3. const express = require('express');
  4. const app = express();//创建一个以express应用,app实际上是一个函数,用于处理请求的函数
  5. app.get('/news', (req, res, next) => {
  6. console.log('handle1');
  7. // res.sendStatus('200');
  8. // res.end();
  9. // throw new Error('abc')
  10. // 相当于throw new Error('abc')
  11. next(new Error('abcd'));
  12. })
  13. app.get('/news', (req, res, next) => {
  14. console.log('handle3');
  15. })
  16. app.use('/news', require('./errMiddleware')); // 使用中间件
  17. app.use(require('./errMiddleware')) // 处理所有的错误请求
  18. app.listen(12306, () => {
  19. console.log('server on 12306 has started')
  20. })
  • 响应指定错误请求:app.use('/news', require('./errMiddleware'));
  • 响应所有错误请求:app.use(require('./errMiddleware')

自定义一个处理静态资源的中间件

  1. 定义

    1. module.exports = (req, res, next) => {
    2. if (req.baseUrl.startsWith('api')) {
    3. next();
    4. } else {
    5. // 说明你想要的是静态资源
    6. if (true) {
    7. res.send('静态资源'); // 请注意,此处并未向后移交后续中间件
    8. } else {
    9. next();
    10. }
    11. }
    12. }
  2. 使用 ```javascript

const express = require(‘express’); const app = express();//创建一个以express应用,app实际上是一个函数,用于处理请求的函数 app.use(require(‘./staticMiddleware’)) app.get(‘/news’, (req, res, next) => { console.log(‘handle1’); // res.sendStatus(‘200’); // res.end(); // throw new Error(‘abc’)

  1. // 相当于throw new Error('abc')
  2. next(new Error('abcd'));

}, (req, res) => { console.log(‘handle2’) })

app.get(‘/news’, (req, res, next) => { console.log(‘handle3’); }) app.use(‘/news’, require(‘./errMiddleware’)); // 使用中间件 app.use(require(‘./errMiddleware’)) // 处理所有的错误请求 app.listen(12306, () => { console.log(‘server on 12306 has started’) }) ```

  • 响应所有的静态资源请求app.use(require('./staticMiddleware'))