How can I catch a rendering error / missing template in node.js using express.js? -
I have a code that will provide a Z template without the specified route, think of this kind of express.static but Calls with res.render url.
app.use (function (req, res, next) {try {res.render (req.url) .substring (1), {title: "no controller", user: req .session.user});} catch (err) {console.log (err) next ();}}}; The problem is that Res.render () is not throwing an error. Instead it is providing an error page. Is there a way to detect missing templates or any rendering errors?
There is a better way to do this, instead of requiring fs and If there is another callback, the render callback will be used: > res.render (my_page_im_not_sure_it_exists, {}, function (error, html) {if (mistake) {res.redirect ('/ 404 '); // file does not exist} and {res.send (html);}});
Comments
Post a Comment