When you pass an error parameter into next
, Express invokes whatever error middleware handler you've installed. The error middleware function has four arguments, so you'd install your own handler as:
app.use(function(err, req, res, next) {
// handle the err from a previous middleware's next(err) call
});
You'd typically add this at the end of your middleware chain so that it handles all other middlewares' errors.
See here for the Express documentation on this.