maudulus
maudulus

Reputation: 11035

Is there a way to dynamically use res.render with a url parameter?

I am trying to dynamically load a page with varying url parameters in the form of ?parameter=12345, but I get the response Failed to lookup view "account/signup?parameter=12345" in views directory, which probably means that it is literally looking for an exact match of that view template, including the parameter aspect. Is there a way that I can render the page with that parameter inside of the following signup code:

exports.getSignup = function(req, res) {
  if (req.user) return res.redirect('/');
  try{
    headerString = req.headers.referer;
    headerString =  headerString.match(/\?(.*)/)[0] 
  }catch(e){
    console.log(e)
  }

  res.render('account/signup' + headerString, {
    title: 'Create Account',
    savedTripId: headerString
  });
};

Upvotes: 0

Views: 1174

Answers (1)

Anubhav
Anubhav

Reputation: 7208

Yo can just render without mentioning the headerString in the url

exports.getSignup = function(req, res) {
  if (req.user) return res.redirect('/');
  try{
    headerString = req.headers.referer;
    headerString =  headerString.match(/\?(.*)/)[0] 
  }catch(e){
    console.log(e)
  }

  res.render('account/signup', {
    title: 'Create Account',
    savedTripId: headerString
  });
};

And since it seems you are using JADE you can do something like this on the client side

form(action="/formsubmit/#{headerString}")
    // form elements
    .
    .
    .

This renders it as

form(action="/formsubmit/1234")
    // form elements
    .
    .
    .

Then when the form is submitted you can get the header back on the server side as follows

app.post('/formsubmit/:headerString', function(req, res){

    var tripId = req.params.headerString;
    var formData = req.body; // if you are using body parser middleware
    // now you know which tripId has to be saved and have the form data. Perform actions accordingly

})

Upvotes: 1

Related Questions