Question came up during the homework review for express-blogpulse: how to redirect a route relative to the Router being used? Example, on POST /post
, how can we best redirect to GET /post/:id
for the newly created post?
Answer: use req.baseUrl
. click for docs: http://expressjs.com/en/4x/api.html#req.baseUrl.
Something like req.redirect(req.baseUrl + "/" + post.id)
req.baseUrl
would be equal to /post
in this case, because of the line app.use("/post", require("controllers/post.js"))