nwkeeley
nwkeeley

Reputation: 1437

node.js and expressjs render a template with a URL param

Currently a user comes to my register page with an invitation ID

Example: http://www.mysite.com/user/register/50e203eb4d37bdfd22000042

The page loads and the form loads etc.... all is great. I then do some back end validation and if validation fails I call the res.render with the messages and the form data:

res.render('user/register', {
    messages: messages 
    , form: form 

});

It all works as expected however when the render occurs it puts the user back to:

http://www.mysite.com/user/register (its missing the invite_id)

How can I pass along a URL param with the res render?

If I do:

res.render('user/register/50e203eb4d37bdfd22000042', {
    messages: messages 
    , form: form 

});

Its actually looking for that 50e203.... file and giving me the 404 not found

Thanks!

Upvotes: 3

Views: 3971

Answers (1)

hunterloftis
hunterloftis

Reputation: 13799

Are you perhaps confusing res.render() with app.get()? It looks like you're trying to specify a route in that second example: res.render('user/register/50e203eb4d37bdfd22000042'... - but res.render doesn't specify routes, you need app[METHOD] for that, where app is an express instance.

In order to handle a GET request to /user/register/someinvite, use:

app.get('/user/register/:invitation', function(req, res, next) {
// in here you can access the invitation id with req.params.invitation
  res.render('user/register');
});

Upvotes: 4

Related Questions