Render specific page on NodeJS error
How can I render a specific page when an error occurs in my Node app开发者_开发知识库? For example catch all errors and render a 404 when they happen...
By the way I am using Express.
You should use app.error()
as described in the guide.
app.get('/error', function(req, res, next){
throw new Error('oops');
});
app.error(function(err, req, res, next){
// do whatever you want
});
// Add an error handling as last piece of middleware
app.use(function(err, req, res, next) {
res.render("404");
});
There is a specific error handling middleware for this express.errorHandler
精彩评论