nodejs express错误处理中间件
文章目录
app.get('/a',function(req,res,next){
res.end('hahah');
next(new Error('错误啦'));
});
app.use('/a',function(err,req,res,next){
console.log('路由错误'+err);
})
//all error中间件
app.use(function(err, req, res, next) {
console.log("Error happens", err.stack);
});
//错误传递,/a的错误处理首先匹配/a那个错误中间件,如果不用next就不会传递到全局错误处理中间件,如果在/a
//错误处理中间件里调用next(err) 那么全局错误中间件也会被执行
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论