1

I'm tring ti redirect user request to another url on post with some additional data.

Lets say user do POST /profile/update After I want to redirect user to another url with some additional data. For example to: POST /test body with some body: {"foo":"bar"}

Is it possible to do ? If yes then how :)

app.post('/profile/update', function(req, res) {
   res.redirect(307, '/test')
   // but dont know how to pass some body here: {"foo":"bar"}
});
  • 1
    https://stackoverflow.com/questions/19035373/how-do-i-redirect-in-expressjs-while-passing-some-context – Chiller Jan 15 '19 at 07:37

1 Answers1

1

From what I understand you are doing it in the right way. Your data should be passed directly to the route /test. That is: if you do req.body in the code of the route /test after having redirected, you should see the data that came from /profile/update. Take a look at this answer and the docs.

If it confuses and you do not want to redirect I suggest you do something different. Create a controller for the route /test (that is, move the code inside the route do a different script called testController.js and inside the route invoke it). Then just go with

app.post('/profile/update', function(req, res) {
   testController.test(req.body);
});
hjbello
  • 643
  • 4
  • 18