Home >Web Front-end >JS Tutorial >A way to handle 404 pages in the NodeJS Express framework_javascript tips

A way to handle 404 pages in the NodeJS Express framework_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:46:312065browse

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:

Copy the code The code 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 wildcards last. In this way, all pages that have not been routed will be taken over by 404.html by default.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn