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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 16:46:31
Original
2118 people have browsed it

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.

Related labels:
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template