使用Express 4处理404错误

时间:2022-04-13 09:12:49

I am using Express 4 and I have about 50 html pages. I'm trying to handle 404 errors but can't figure out how. I don't want to manually define all the routers within node. Is there a way to dynamically redirect to a 404 Jade template if a page doesn't exist?

我正在使用Express 4,我有大约50个html页面。我正在尝试处理404错误,但无法弄清楚如何。我不想手动定义节点内的所有路由器。如果页面不存在,有没有办法动态重定向到404 Jade模板?

I tried this code but didn't work:

我试过这段代码但是没有用:

app.enable('verbose errors');
app.set('port', 3000);

app.use(express.static(__dirname + '/html/'));

var server = http.createServer(app);
server.listen(app.get('port'), function() {
    console.log('ONLINE !');
});

app.use(function(req, res, next) {
    console.log('GET ' + req.originalUrl)
    console.log('At %d', Date.now());
    next();
});

// Handle 404
app.use(function(req, res, next) {
    if(req.accepts('html') && res.status(404)) {
        res.render('404.jade');
        return;
    }
});

1 个解决方案

#1


9  

This is working for me:

这对我有用:

var express = require('express');
var app = express();

app.use(express.static('public'));

app.get('/', function (req, res) {
    res.send('Hello World!');
});

app.get('/employee', function (req, res) {
    res.send('Employee route !!');
});


// Handle 404 - Keep this as a last route
app.use(function(req, res, next) {
    res.status(404);
    res.send('404: File Not Found');
});

app.listen(3000, function () {
    console.log('Example app listening on port 3000!');
});

Folder structure,

使用Express 4处理404错误

Now when we issue the request like this

现在我们发出这样的请求

http://localhost:3000/sample

This has been handled by the middleware.

这已由中间件处理。

UPDATE

The way to show the html files without writing the get request is just another middleware like this

在不编写get请求的情况下显示html文件的方法就是这样的另一个中间件

app.use(express.static('public'));
app.use(express.static('views'));

Add the 'views' middleware exactly after the 'public'.

在'public'之后添加'views'中间件。

Now if we give

现在,如果我们给

http://localhost:3000/index.html

The page is rendered.

页面呈现。

#1


9  

This is working for me:

这对我有用:

var express = require('express');
var app = express();

app.use(express.static('public'));

app.get('/', function (req, res) {
    res.send('Hello World!');
});

app.get('/employee', function (req, res) {
    res.send('Employee route !!');
});


// Handle 404 - Keep this as a last route
app.use(function(req, res, next) {
    res.status(404);
    res.send('404: File Not Found');
});

app.listen(3000, function () {
    console.log('Example app listening on port 3000!');
});

Folder structure,

使用Express 4处理404错误

Now when we issue the request like this

现在我们发出这样的请求

http://localhost:3000/sample

This has been handled by the middleware.

这已由中间件处理。

UPDATE

The way to show the html files without writing the get request is just another middleware like this

在不编写get请求的情况下显示html文件的方法就是这样的另一个中间件

app.use(express.static('public'));
app.use(express.static('views'));

Add the 'views' middleware exactly after the 'public'.

在'public'之后添加'views'中间件。

Now if we give

现在,如果我们给

http://localhost:3000/index.html

The page is rendered.

页面呈现。