When using Express, routing is one of the things that is most confused about me. It is known that using app.get('*') can handle all pages, but this way, static files are ignored except for custom routes. Recently, when I was writing a gadget, I found a solution:
The code copy is as follows:
var express = require('express'),
router = require('./routes');
var app = module.exports = express.createServer();
// Configuration
app.configure(function () {
// ...
// Don't write the order in reverse
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
// Other routers...
// 404
app.get('*', function(req, res){
res.render('404.html', {
title: 'No Found'
})
});
Put the wildcards at the end. In this way, all pages that have not been routed will be taken over by default by 404.html.