Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to redirect from one URL request to another 'POST' request, like this:

var app = require('express')();

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

app.post('/test', function(req, res) {
  res.send('/test page');
});

app.listen(3000, function() {
  console.log('listenning on port:3000');
});

However, I can't redirect to '/test' page because it is a POST request.
So what should I do to make the redirection work, keeping the '/test' request POST?

like image 652
neolicd Avatar asked Aug 07 '16 01:08

neolicd


People also ask

How do I redirect POST Express?

The res. redirect() function lets you redirect the user to a different URL by sending an HTTP response with status 302. The HTTP client (browser, Axios, etc.) will then "follow" the redirect and send an HTTP request to the new URL as shown below.

How do I forward a Node.js request?

send('Req OK'); }); function processRequest(req){ console. log("request processed"); } function sendRequestToOtherEndPoint(req){ //magic here :) } When this server receive a get request in port 3000, it process request information and it must forward the same requesto to another end point.


Video Answer


3 Answers

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

like image 72
ldg Avatar answered Oct 21 '22 12:10

ldg


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
)
like image 4
Radagast the Brown Avatar answered Oct 21 '22 13:10

Radagast the Brown


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

like image 3
pshx Avatar answered Oct 21 '22 12:10

pshx