express define 404 code example
Example 1: set 404 handling via express in node
app.use(function(req, res, next){
res.status(404);
if (req.accepts('html')) {
res.render('404', { url: req.url });
return;
}
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
res.type('txt').send('Not found');
});
Example 2: express 404
app.use(function(req, res, next){
res.status(404);
if (req.accepts('html')) {
res.sendFile('index.html');
return;
}
if (req.accepts('json')) {
res.send({
status: 404,
error: 'Not found'
});
return;
}
res.type('txt').send('404 - Not found');
});