我可以重用express.js错误视图吗? [英] Can I reuse the express.js error view?
本文介绍了我可以重用express.js错误视图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当express无法找到视图时,它会出错,(在调试模式下)会呈现一个漂亮的页面。有没有一个方便的方法来重新使用这个视图为我自己的错误消息?
When express can't find a view it errors out and (in debug mode) renders a nice looking page. Is there a convenient way to re-use that view for my own error messages?
推荐答案
我不知道如何重用该视图,但您可以使用error.stack渲染堆栈跟踪。
I don't know how to reuse the view but you can use the error.stack to render the stack trace.
我有这样的东西:
app.all( '*', function(req, res){
var code = 404;
res.local('error', { type: 'http', code: code });
res.local('code', code );
res.render('errors/index', { status: code } );
});
app.error(function(err, req, res, next){
var code;
if( err.type === 'http' ){
code = err.error;
}
else {
code = 500;
};
if(err){
res.local('stack', err.stack || JSON.stringify(err, null, 2) );
};
res.local('code', code );
res.render('errors/index', { status: code } );
});
如果我需要手动设置为404,请在我的视图中执行以下操作:
If I need to manually set like a 404 I do the following in my views:
next( {type:'http', error: 404} );
这是我的观点中有类型检查的地方。
That's where there is a type check in my views.
这篇关于我可以重用express.js错误视图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文