Javascript Node.js/Express - 找不到页面时呈现错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7641622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js/Express - Render error when page not found
提问by tuddy
I have the following controller/route definition in Node.js (using Express and Mongoose). What would be the leanest most appropriate way to handle Error when the user requests a page that does not exist?
我在 Node.js 中有以下控制器/路由定义(使用 Express 和 Mongoose)。当用户请求一个不存在的页面时,处理错误的最精简最合适的方法是什么?
app.get('/page/:pagetitle', function(req, res) {
Page.findOne({ title: req.params.pagetitle}, function(error, page) {
res.render('pages/page_show.ejs',
{ locals: {
title: 'ClrTouch | ' + page.title,
page:page
}
});
});
});
It currently breaks my app. I believe because I'm not doing anything with the error i'm just passing it to the view like a success?
它目前破坏了我的应用程序。我相信是因为我没有对错误做任何事情,我只是像成功一样将它传递给视图?
TypeError: Cannot read property 'title' of null
Thanks much.
非常感谢。
回答by blockchaindev
Check out the express error-pagesexample. The principle is to register your app routes first, then you register a catch all 404 handler for all other requests that do not map to a route. Finally, a 500 handler is registered, as follows:
查看快速错误页面示例。原则是先注册您的应用程序路由,然后为所有其他未映射到路由的请求注册一个 catch all 404 处理程序。最后,注册了一个 500 处理程序,如下所示:
// "app.router" positions our routes
// specifically above the middleware
// assigned below
app.use(app.router);
// Since this is the last non-error-handling
// middleware use()d, we assume 404, as nothing else
// responded.
app.use(function(req, res, next){
// the status option, or res.statusCode = 404
// are equivalent, however with the option we
// get the "status" local available as well
res.render('404', { status: 404, url: req.url });
});
// error-handling middleware, take the same form
// as regular middleware, however they require an
// arity of 4, aka the signature (err, req, res, next).
// when connect has an error, it will invoke ONLY error-handling
// middleware.
// If we were to next() here any remaining non-error-handling
// middleware would then be executed, or if we next(err) to
// continue passing the error, only error-handling middleware
// would remain being executed, however here
// we simply respond with an error page.
app.use(function(err, req, res, next){
// we may use properties of the error object
// here and next(err) appropriately, or if
// we possibly recovered from the error, simply next().
res.render('500', {
status: err.status || 500
, error: err
});
});
回答by Nican
One of the major problems with Node.JS is that there is no clean error catching. The conventional way is usually for every callback function, the first argument is the not null if there is an error, so for example:
Node.JS 的主要问题之一是没有干净的错误捕获。常规方式通常是对于每个回调函数,如果有错误,第一个参数是not null,例如:
function( error, page ){
if( error != null ){
showErrorPage( error, req, res );
return;
}
...Page exists...
}
Things can get ugly after a while with too many callbacks, and I recommend using something like async, so that if there is one error, it goes directly to an error callback.
回调过多一段时间后事情会变得很糟糕,我建议使用async 之类的东西,这样如果有一个错误,它就会直接进入错误回调。
EDIT: You can also use express error handling.
编辑:您还可以使用快速错误处理。