1
2// Dopo tutte le altre route
3
4app.get('*', function(req, res, next) {
5 var err = new Error();
6 err.status = 404;
7 next(err);
8});
9
10app.use(function(err, req, res, next) {
11 if (err.status === 404) {
12 var data = {
13 title: '404 Not Found',
14 content: 'Oops, page not found!';
15 };
16 res.render('pages/404', data);
17 } else {
18 return next();
19 }
20});
21
22
1app.use(function(req, res, next){
2 res.status(404);
3
4 // respond with html page
5 if (req.accepts('html')) {
6 res.render('404', { url: req.url });
7 return;
8 }
9
10 // respond with json
11 if (req.accepts('json')) {
12 res.send({ error: 'Not found' });
13 return;
14 }
15
16 // default to plain-text. send()
17 res.type('txt').send('Not found');
18});