Fel*_*lix 261
我发现这个例子非常有帮助:
https://github.com/visionmedia/express/blob/master/examples/error-pages/index.js
所以它实际上就是这个部分:
// "app.router" positions our routes
// above the middleware defined below,
// this means that Express will attempt
// to match & call routes _before_ continuing
// on, at which point we assume it's a 404 because
// no route has handled the request.
app.use(app.router);
// Since this is the last non-error-handling
// middleware use()d, we assume 404, as nothing else
// responded.
// $ curl http://localhost:3000/notfound
// $ curl http://localhost:3000/notfound -H "Accept: application/json"
// $ curl http://localhost:3000/notfound -H "Accept: text/plain"
app.use(function(req, res, next){
res.status(404);
// respond with html page
if (req.accepts('html')) {
res.render('404', { url: req.url });
return;
}
// respond with json
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});
Run Code Online (Sandbox Code Playgroud)
Alf*_*red 143
我认为您应首先定义所有路线,并作为最后一条路线添加
//The 404 Route (ALWAYS Keep this as the last route)
app.get('*', function(req, res){
res.status(404).send('what???');
});
Run Code Online (Sandbox Code Playgroud)
一个有效的示例应用程序:
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');
Run Code Online (Sandbox Code Playgroud)
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?
Run Code Online (Sandbox Code Playgroud)
Gan*_*mar 33
您可以将中间件放在抛出NotFound错误的最后位置,
甚至可以直接呈现404页面:
app.use(function(req,res){
res.status(404).render('404.jade');
});
Run Code Online (Sandbox Code Playgroud)
Sus*_*pta 31
上面的答案是好的,但是其中一半你不会得到404作为你的HTTP状态代码返回,而在另一半,你将无法有自定义模板渲染.在Expressjs中拥有自定义错误页面(404)的最佳方法是
app.use(function(req, res, next){
res.status(404).render('404_error_template', {title: "Sorry, page not found"});
});
Run Code Online (Sandbox Code Playgroud)
将此代码放在所有URL映射的末尾.
Viv*_*pai 11
在 app.js 的最后一行,只需要放置这个函数。这将覆盖默认的 page-not-found 错误页面:
app.use(function (req, res) {
res.status(404).render('error');
});
Run Code Online (Sandbox Code Playgroud)
它将覆盖所有没有有效处理程序的请求并呈现您自己的错误页面。
你的问题的答案是:
app.use(function(req, res) {
res.status(404).end('error');
});
Run Code Online (Sandbox Code Playgroud)
而且,关于为什么它是最好的方式一个伟大的文章在这里。
| 归档时间: |
|
| 查看次数: |
206941 次 |
| 最近记录: |