2

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

Lets say the user does POST /profile/update
Afterwards, I want to redirect the user to another url with some additional data.
e.g., POST /test body with a body, {"foo": "bar"}

How can I do this?

app.post('/profile/update', function(req, res) { res.redirect(307, '/test') // but dont know how to pass some body here: {"foo":"bar"} }); 
1

1 Answer 1

1

From what I understand you are doing it in the right way. Your data should be passed directly to the route /test. i.e., 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 is confusing and you do not want to redirect, I suggest you do something different. Create a controller for the route /test (i.e., 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); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.