Express 3 error middleware not being called

前端 未结 4 1937
温柔的废话
温柔的废话 2021-02-18 18:46

I am trying to setup error handling for my express app and running into the following problem.

I defined an error middleware and add it as the last middleware:



        
4条回答
  •  眼角桃花
    2021-02-18 19:06

    Updated answer for Express 4 users from the Express 4 docs. See example from docs below. Note that app.router is deprecated and no longer used. I also added a dummy route to make the ordering clear:

    "You define error-handling middleware last, after other app.use() and routes calls; For example:

    var bodyParser = require('body-parser');
    
    app.use(bodyParser());
    app.get('/', function(req, res) {
        res.send('hello world');
    })
    app.use(function(err, req, res, next) {
      // logic
    });
    

    "

提交回复
热议问题