0

So, I am trying to get different data at different scenarios for the same URL. Here is my code.

1st scenario:

app.get('/upload', function(req, res) {
    res.render('upload', {data: 'a'});
});

2nd scenario:

res.redirect('/upload');
app.get('/upload', function(req, res) {
    res.render('upload', {data: 'b'});
});

However, for some reason, it always executes the 1st scenario (only passes data: 'a'). How to prioritize each of the get method for a specific situation?

2
  • What is the difference between 1st and 2nd scenario? What is the situation how can you check it? Commented May 4, 2018 at 7:24
  • @hurricane 1st scenario is just when we want to access the particular page. 2nd scenario is basically after I submitted a form, I want to redirect to the same page but with different content. I'm trying to access the different content by passing the data. Commented May 4, 2018 at 7:25

1 Answer 1

1

First of all you can not use same string name for two different job.

You can do that with params or query. I have created an example with params for you. You can change switch case for your scenario.

Params

app.get('/upload/:scenario', function(req, res) {
  let resultData = {};
  switch (req.params.scenario) {
    case '1':
      resultData = {
        data: 'a'
      };
      break;
    case '2':
      resultData = {
        data: 'b'
      };
      break;
    default:
      resultData = {
        error: 'wrong id'
      };
  }
  res.render('upload', resultData);
  // http://localhost/api/upload/1 -> {"data":"a"}
  // http://localhost/api/upload/2 -> {"data":"b"}
});
Sign up to request clarification or add additional context in comments.

1 Comment

What do yo mean by 'same string name for two different job'?

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.