NodeJS Express framework handles 404 pages in one way
- 2020-03-30 03:05:08
- OfStack
Routing is one of my biggest confusions when using Express. Know that all pages can be processed with app.get('*'), but that static files are ignored except for custom routes. Recently, while writing a gadget, I found a solution:
var express = require('express'),
router = require('./routes');
var app = module.exports = express.createServer();
// Configuration
app.configure(function () {
// ...
//Don't reverse the order
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
//Other router...
// 404
app.get('*', function(req, res){
res.render('404.html', {
title: 'No Found'
})
});
Put the wildcard at the end. So all pages that are not routed are taken over by default by 404.html.