Node.js with Express: how to redirect a POST request Node.js with Express: how to redirect a POST request express express

Node.js with Express: how to redirect a POST request


You can do this:

app.post('/', function(req, res) {  res.redirect(307, '/test');});

Which will preserve the send method.

For reference, the 307 http code spec is:

307 Temporary Redirect (since HTTP/1.1) In this occasion, the request should be repeated with another URI, but future requests can still use the original URI.2 In contrast to 303, the request method should not be changed when reissuing the original request. For instance, a POST request must be repeated using another POST request.

For more info, see: http://www.alanflavell.org.uk/www/post-redirect.html


Keep in mind the middleware architecture: Each handler may manipulate the context, and either respond - or - call next().

By this premise, the express router is basically a middleware function you may use after "correcting" the url.

(BTW, the request app is also a function, although I'm not sure if I recommend going back so early in the chain)

Here's a kind'a example:

const router = new require('express').Router()const user = require('../model/user') //assume user implements://  user.byId(id) -> Promise<user>//  user.byMail(email) -> Promise<user>const reqUser = userPromise => (req, res, next) =>   req.user     ? next()     : userPromise(req)       .then(user => { req.user = user })       .then(next, next)//assume the sever that uses this router has a //standard (err, req, res, next) handler in the end of the chain...const byId = reqUser( req => user.byId(req.params.id) )const byMail = reqUser( req => user.byMail(req.params.mail) )router.post('/by-id/:id/friends',  byId,  (req, res) => res.render('user-friends', req.user))router.post('/by-email/:email/friends',  byMail,  (req, res, next) => {     req.url = `/by-id/${req.user.id}/friends`     next()  },   router)


The only difference between 307 and 302 is that 307 guarantees that the method and the body will not be changed when the redirected request is made.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307