Skip to content

Instantly share code, notes, and snippets.

@connorjclark
Created April 25, 2017 18:19
Show Gist options
  • Save connorjclark/229af178e54b72c8436e81180d809d82 to your computer and use it in GitHub Desktop.
Save connorjclark/229af178e54b72c8436e81180d809d82 to your computer and use it in GitHub Desktop.
req.baseUrl

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"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment