在 https://github.com/expressjs/express/blob/master/lib/router/index.js#L46
function router(req, res, next) {
router.handle(req, res, next);
}
express router不會catch error,所以error handler定義在router內無法作用
要直接定義在app那層,參考doc Error-handling middleware
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
另外是順序也很重要,next(err)會依照順序往下找,因此error handler應該定義在最後面
參考來源:
https://stackoverflow.com/questions/45431595/express-error-handler-inside-router-doesnt-work