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?

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

Comments

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);
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.