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?
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.
Check out the express error-pages example. 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: