与Sails: 404 pages won't use the default layout?类似,但该答案仅适用于0.10.x,此时此选项不适用。我本来要求澄清,但我还没有足够的声誉。
我的目标是使用404语言和搜索栏来提供我的应用程序的默认布局,但每次我尝试修改404.ejs页面并请求myapp.com/routethatdoesntexist时,我得到的回答是“无法使用”得到/路线痛苦的'
我觉得这有点像:
var view = 'new404';
res.locals.layout = 'layout';
res.render(view, result, function (err) {
if (err) {
return express404Handler();
}
res.render(view);
});
但是这个结果与使用提供的404.ejs文件没什么不同。我错过了什么?
[编辑]
尝试下面的建议,我将404.js文件更改为:
var view = '404test';
res.view('404test', {layout: '404layout_test'});
和404.ejs:
大麻烦,小中国
404layout_test.ejs是我们当前和正常运行的布局文件的1副本。无论我选择哪种布局,错误信息都是相同的:
{
"path": {
"message": "'/Users/brandt/midas/views/404layout_test.ejs'",
"stack": "Error: '/Users/brandt/midas/views/404layout_test.ejs'\n at Object.serverErrorOccurred [as 500] (/Users/brandt/midas/config/500.js:28:28)\n at ServerResponse.respond500 (/Users/brandt/midas/node_modules/sails/lib/hooks/request/index.js:134:21)\n at /Users/brandt/midas/node_modules/sails/lib/hooks/views/index.js:301:17\n at Function.app.render (/Users/brandt/midas/node_modules/sails/node_modules/express/lib/application.js:508:5)\n at ServerResponse.res.render (/Users/brandt/midas/node_modules/sails/node_modules/express/lib/response.js:798:7)\n at ServerResponse._addResViewMethod.res.view (/Users/brandt/midas/node_modules/sails/lib/hooks/views/index.js:297:15)\n at Object.pageNotFound [as 404] (/Users/brandt/midas/config/404.js:25:7)\n at Object.handle (/Users/brandt/midas/node_modules/sails/lib/express/index.js:198:21)\n at next (/Users/brandt/midas/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15)\n at Object.favicon [as handle] (/Users/brandt/midas/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/favicon.js:77:7)"
},
"INVALID_EXPRESSION_ERR": {
"message": "51",
"stack": "Error: 51\n at Object.serverErrorOccurred [as 500] (/Users/brandt/midas/config/500.js:28:28)\n at ServerResponse.respond500 (/Users/brandt/midas/node_modules/sails/lib/hooks/request/index.js:134:21)\n at /Users/brandt/midas/node_modules/sails/lib/hooks/views/index.js:301:17\n at Function.app.render (/Users/brandt/midas/node_modules/sails/node_modules/express/lib/application.js:508:5)\n at ServerResponse.res.render (/Users/brandt/midas/node_modules/sails/node_modules/express/lib/response.js:798:7)\n at ServerResponse._addResViewMethod.res.view (/Users/brandt/midas/node_modules/sails/lib/hooks/views/index.js:297:15)\n at Object.pageNotFound [as 404] (/Users/brandt/midas/config/404.js:25:7)\n at Object.handle (/Users/brandt/midas/node_modules/sails/lib/express/index.js:198:21)\n at next (/Users/brandt/midas/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15)\n at Object.favicon [as handle] (/Users/brandt/midas/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/favicon.js:77:7)"
},
"TYPE_ERR": {
"message": "52",
"stack": "Error: 52\n at Object.serverErrorOccurred [as 500] (/Users/brandt/midas/config/500.js:28:28)\n at ServerResponse.respond500 (/Users/brandt/midas/node_modules/sails/lib/hooks/request/index.js:134:21)\n at /Users/brandt/midas/node_modules/sails/lib/hooks/views/index.js:301:17\n at Function.app.render (/Users/brandt/midas/node_modules/sails/node_modules/express/lib/application.js:508:5)\n at ServerResponse.res.render (/Users/brandt/midas/node_modules/sails/node_modules/express/lib/response.js:798:7)\n at ServerResponse._addResViewMethod.res.view (/Users/brandt/midas/node_modules/sails/lib/hooks/views/index.js:297:15)\n at Object.pageNotFound [as 404] (/Users/brandt/midas/config/404.js:25:7)\n at Object.handle (/Users/brandt/midas/node_modules/sails/lib/express/index.js:198:21)\n at next (/Users/brandt/midas/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15)\n at Object.favicon [as handle] (/Users/brandt/midas/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/favicon.js:77:7)"
}
}
将res.view设置为: res.view('404test',{layout:false});
成功,但没有布局。
[编辑#3]
我跟踪了它的存在<% - ...%>在默认布局的头部。
答案 0 :(得分:1)
这应该标记为Sails: 404 pages won't use the default layout?的副本,但如果它永远滞留在关闭投票队列中:
要在Sails v0.9.x中使用自定义404页面的布局,请编辑 config / 404.js 文件以使用res.view
代替res.render
,并在第二个参数中提供您的布局名称(或者,如果您想在 views / layout.ejs 中使用默认布局,请不要这样做):
res.view("my404page", {layout: "myFancyLayout"})