0
module.exports = function(app) {
    try{
         app.get('/:path/:id', function (req, res) {
           res.render(req.params.path+'/'+req.params.id, { id: req.params.id });
         }); 

    }
    catch(e){
         console.error(e);
    }
};

if res.render page not found, how to redirect to another page?

kingnight
  • 720
  • 6
  • 10

2 Answers2

1

The easiest way would be to redirect to your 404 route when an error occurs in template render.

like

app.get('/:path/:id', function (req, res) {

   res.render(req.params.path+'/'+req.params.id,{id:req.params.id},function(err,html){
        if(err) {
            //error in rendering template o redirect to 404 page
            res.redirect('/404');
        } else {
            res.end(html);
        }
   });

});

reference post: How can I catch a rendering error / missing template in node.js using express.js?

Community
  • 1
  • 1
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
0

Why don't you just create a function which will handle the 404 page. I.e. something like this:

var show404Page = function(res) {
    var html = "404 page";
    res.end(html);
}
module.exports = function(app) {
    try{
         app.get('/:path/:id', function (req, res) {
            res.render(req.params.path+'/'+req.params.id, { id: req.params.id });
         }); 
    }
    catch(e){
        console.error(e);
        show404Page(res);
    }
};
Krasimir
  • 13,306
  • 3
  • 40
  • 55