开发者

Render specific page on NodeJS error

开发者 https://www.devze.com 2023-04-12 03:28 出处:网络
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...

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

0

精彩评论

暂无评论...
验证码 换一张
取 消