我不知道这样做的函数,有人知道吗?


当前回答

我认为你应该首先定义你所有的路线,然后作为最后的路线添加

//The 404 Route (ALWAYS Keep this as the last route)
app.get('*', function(req, res){
  res.status(404).send('what???');
});

一个示例应用程序的工作:

app.js:

var express = require('express'),
    app = express.createServer();

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

app.get('/', function(req, res){
  res.send('hello world');
});

//The 404 Route (ALWAYS Keep this as the last route)
app.get('*', function(req, res){
  res.send('what???', 404);
});

app.listen(3000, '127.0.0.1');

alfred@alfred-laptop:~/node/stackoverflow/6528876$ mkdir public
alfred@alfred-laptop:~/node/stackoverflow/6528876$ find .
alfred@alfred-laptop:~/node/stackoverflow/6528876$ echo "I don't find a function for that... Anyone knows?" > public/README.txt
alfred@alfred-laptop:~/node/stackoverflow/6528876$ cat public/README.txt 

.
./app.js
./public
./public/README.txt

alfred@alfred-laptop:~/node/stackoverflow/6528876$ curl http://localhost:3000/
hello world
alfred@alfred-laptop:~/node/stackoverflow/6528876$ curl http://localhost:3000/README.txt
I don't find a function for that... Anyone knows?

其他回答

上面的答案很好,但其中一半的答案不会返回404作为HTTP状态代码,而另一半答案则不能呈现自定义模板。在Expressjs中拥有自定义错误页面(404)的最佳方法是

app.use(function(req, res, next){
    res.status(404).render('404_error_template', {title: "Sorry, page not found"});
});

将此代码放在所有URL映射的末尾。

覆盖express中的所有HTTP动词

为了覆盖所有HTTP动词和所有剩余路径,您可以使用:

app.all('*', cb)

最终的解决方案是这样的:

app.all('*', (req, res) =>{
    res.status(404).json({
        success: false,
        data: '404'
    })
})

你不应该忘记把路由器放在最后。 因为路由器的顺序很重要。

上面的代码对我不起作用。

所以我找到了一个真正有效的新解决方案!

app.use(function(req, res, next) {
    res.status(404).send('Unable to find the requested resource!');
});

或者您甚至可以将其呈现到404页面。

app.use(function(req, res, next) {
    res.status(404).render("404page");
});

希望这对你有所帮助!

你可以把一个中间件放在最后一个位置,它会抛出一个NotFound错误, 甚至直接渲染404页面:

app.use(function(req,res){
    res.status(404).render('404.jade');
});

如果你想从你的函数(路由)重定向到错误页面,那么做以下事情-

Add general error messages code in your app.js - app.use(function(err, req, res, next) { // set locals, only providing error in development res.locals.message = err.message res.locals.error = req.app.get('env') === 'development' ? err : {} // render the error page // you can also serve different error pages // for example sake, I am just responding with simple error messages res.status(err.status || 500) if(err.status === 403){ return res.send('Action forbidden!'); } if(err.status === 404){ return res.send('Page not found!'); } // when status is 500, error handler if(err.status === 500) { return res.send('Server error occured!'); } res.render('error') }) In your function, instead of using a error-page redirect you can use set the error status first and then use next() for the code flow to go through above code - if(FOUND){ ... }else{ // redirecting to general error page // any error code can be used (provided you have handled its error response) res.status(404) // calling next() will make the control to go call the step 1. error code // it will return the error response according to the error code given (provided you have handled its error response) next() }