Express.Application error code example
Example 1: how to handle all error of all router in express
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error.ejs', {
message: err.message,
error: err
});
});
}
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
Example 2: error * route
app.get('*', (request, response) => response.status(404).send('This route does not exist'));