Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js/Express - Render error when page not found

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.

like image 961
tuddy Avatar asked Oct 03 '11 22:10

tuddy


People also ask

How does Express handle 404 error?

In Express, 404 responses are not the result of an error, so the error-handler middleware will not capture them. This behavior is because a 404 response simply indicates the absence of additional work to do; in other words, Express has executed all middleware functions and routes, and found that none of them responded.

What is Express () in node JS?

Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.


1 Answers

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:

// "app.router" positions our routes  // specifically above the middleware // assigned below  app.use(app.router);  // Since this is the last non-error-handling // middleware use()d, we assume 404, as nothing else // responded.  app.use(function(req, res, next){   // the status option, or res.statusCode = 404   // are equivalent, however with the option we   // get the "status" local available as well   res.render('404', { status: 404, url: req.url }); });  // error-handling middleware, take the same form // as regular middleware, however they require an // arity of 4, aka the signature (err, req, res, next). // when connect has an error, it will invoke ONLY error-handling // middleware.  // If we were to next() here any remaining non-error-handling // middleware would then be executed, or if we next(err) to // continue passing the error, only error-handling middleware // would remain being executed, however here // we simply respond with an error page.   app.use(function(err, req, res, next){   // we may use properties of the error object   // here and next(err) appropriately, or if   // we possibly recovered from the error, simply next().   res.render('500', {       status: err.status || 500     , error: err   }); }); 
like image 181
blockchaindev Avatar answered Oct 16 '22 18:10

blockchaindev