我不知道这样做的函数,有人知道吗?
当前回答
你可以把一个中间件放在最后一个位置,它会抛出一个NotFound错误, 甚至直接渲染404页面:
app.use(function(req,res){
res.status(404).render('404.jade');
});
其他回答
我认为你应该首先定义你所有的路线,然后作为最后的路线添加
//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?
我发现这个例子很有帮助:
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.json({ error: 'Not found' });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});
https://github.com/robrighter/node-boilerplate/blob/master/templates/app/server.js
这就是node-boilerplate所做的。
如果你想从你的函数(路由)重定向到错误页面,那么做以下事情-
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() }
// Add this middleware
// error handler
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
res.status(err.status || 500);
res.render('error');
});
推荐文章
- 检查已安装的angular-cli版本?
- 如何将Blob转换为JavaScript文件
- 如何用Express/Node以编程方式发送404响应?
- 如何安装包从github回购在纱线
- 什么时候.then(success, fail)被认为是承诺的反模式?
- 自动HTTPS连接/重定向使用node.js/express
- 在nodejs http中body在哪里。得到回应?
- 如何在猫鼬排序?
- Nodemon错误:“已达到文件监视器数量的系统限制”
- 如何使webpack开发服务器运行在端口80和0.0.0.0使其公开访问?
- 如何在node.js模块中访问和测试内部(非导出)函数?
- 如何将base64编码的映像保存到磁盘?
- 在用nodejs和express创建的REST API中设置响应状态和JSON内容的正确方法
- 如何获得请求路径与表达请求对象
- 节点和错误:EMFILE,打开的文件太多