Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

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');
});
question from:https://stackoverflow.com/questions/38810114/node-js-with-express-how-to-redirect-a-post-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...