使用Express的Node.js:如何重定向POST请求 [英] Node.js with Express: how to redirect a POST request

查看:618
本文介绍了使用Express的Node.js:如何重定向POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个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.

有关更多信息,请参见: http://www.alanflavell.org. uk/www/post-redirect.html

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

这篇关于使用Express的Node.js:如何重定向POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆