Home >Web Front-end >JS Tutorial >A way to handle 404 pages in the NodeJS Express framework_javascript tips
Routing is one of the most confusing things for me when using Express. You know that you can use app.get('*') to process all pages, but in this way, except for other customized routes, static files are ignored. Recently, when I was writing a small tool, I found a solution:
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 wildcards last. In this way, all pages that have not been routed will be taken over by 404.html by default.