Justin
Justin

Reputation: 4539

After post saved to database, how do I route to another ejs page

How can I load another page after a post in node.js with ejs.

//router
function router() {
   ticketRouter.route("/create/submit") // tied to submit from one ejs
      .post(dataControllerThatTakesCareOfThis) // works fine

    // either in here, or even the controller I suppose I want to load another page after the submit takes place?
     res.redirect(200, '/load:postSubmit') // doesn't work
     res.send({ redirect: '/load:postSubmit' }) // great for ajax response only

}

module.exports = router;

So is the above something that you can do in node.js without having to go back out to the client with some information and then have it ask the question in order to process it? Is there a 'redirect' or a way to route the response like this?

Thanks

Upvotes: 0

Views: 88

Answers (1)

Lorpy
Lorpy

Reputation: 26

Supposing you're using ExpressJS and EJS is your Template Engine :

You should handle your redirection into the dataControllerThatTakesCareOfThis function.

These two lines :

// either in here, or even the controller I suppose I want to load another page after the submit takes place?
 res.redirect(200, '/load:postSubmit') // doesn't work
 res.send({ redirect: '/load:postSubmit' }) // great for ajax response only

Won't work because they are using the res object which is not defined here but in the callback of the Post method of the Router object.

More info in the express documentation

Upvotes: 1

Related Questions