2011-10-10 46 views

回答

1
// Add an error handling as last piece of middleware 
app.use(function(err, req, res, next) { 
    res.render("404"); 
}); 

有一种特定的错误处理的中间件此express.errorHandler

+0

获得一个“对象#还没有呈现方法”错误 – user971956

+0

@ user971956我忘了参数的顺序,固定它现在。 '(err,req,res,next)' – Raynos

+0

很好,谢谢! – user971956

2

您应该使用app.error(),如the guide中所述。

app.get('/error', function(req, res, next){ 
    throw new Error('oops'); 
}); 
app.error(function(err, req, res, next){ 
    // do whatever you want 
});