node js express error page code example
Example 1: express generator error handling
app.get('/', function (req, res) {
throw new Error('BROKEN') // Express will catch this on its own.
})
Example 2: express 404
app.use(function(req, res, next){
res.status(404);
// respond with html page
if (req.accepts('html')) {
res.sendFile('index.html');
return;
}
// respond with json
if (req.accepts('json')) {
res.send({
status: 404,
error: 'Not found'
});
return;
}
// default to plain-text. send()
res.type('txt').send('404 - Not found');
});