Catch "Failed to look up view" error and serve 404
express, locomotivejs, node.js
Solution
It might look something like this:
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' });
}
};
Problem
I am currently routing every page to my pagesController that cannot be found in previous routes by including this line in routes.js: ``` this.match('/:page', { controller: 'pages', action: 'show' }); ``` I had the idea to let my PagesController handle serving a 404 if not found: ``` 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; }; ``` But my idea is failing. The error cannot be caught, so the fatal still gets served. Should I append a fn to the render call? With what arguments? How does it work? (/simple questions).