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:
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
});
"