neolicd neolicd - 1 year ago 546
Node.js Question

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) {
});'/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?

ldg ldg
Answer Source

You can do this:'/', 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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download