Spo*_*ork 4 node.js express locomotivejs
我目前通过在routes.js中包含此行,将每个页面路由到我的pagesController,这在以前的路由中找不到:
this.match('/:page', { controller: 'pages', action: 'show' });
Run Code Online (Sandbox Code Playgroud)
如果没有找到,我有想法让我的PagesController句柄服务404:
PagesController.show = function() {
var page = this.param('page');
if(page != undefined){
page = page.replace(".html","");
try {
this.render("./"+page);
} catch(error){ //Failed to look up view -- not working at the moment =(
this.redirect({action : "404"});
};
}
return;
};
Run Code Online (Sandbox Code Playgroud)
但我的想法是失败的.错误无法捕获,因此致命的仍然可以获得.我应该将fn附加到渲染调用吗?有什么论据?它是如何工作的?(/简单的问题).
它可能看起来像这样:
PagesController.show = function() {
var self = this;
var page = this.param('page');
if (page !== undefined) {
page = page.replace('.html', '');
this.render("./" + page, function(err, html) {
if (! err)
return self.res.send(html);
self.redirect({ action : '404' });
});
} else {
// this will probably never be called, because `page`
// won't be undefined, but still...
this.redirect({ action : '404' });
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1047 次 |
| 最近记录: |