我想从一个URL请求重定向到另一个"POST"请求,例如:
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'); });
但是,我无法重定向到"/test"页面,因为它是POST请求. 那么我应该怎么做才能使重定向工作,同时保持"/test"请求POST?
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?
推荐答案您可以执行以下操作:
app.post('/', function(req, res) { res.redirect(307, '/test'); });将保留send方法.
作为参考,307 http代码规范为:
For reference, the 307 http code spec is:
307临时重定向(自HTTP/1.1起)在这种情况下,请求 应该与另一个URI重复,但是以后的请求仍然可以使用 原始URI.2与303相比,请求方法不应 重新发出原始请求时进行更改.例如,POST 该请求必须使用另一个POST请求重复进行.
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.
有关更多信息,请参见: www.alanflavell. uk/www/post-redirect.html
For more info, see: www.alanflavell.uk/www/post-redirect.html
更多推荐
使用Express的Node.js:如何重定向POST请求
发布评论