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

查看:45
本文介绍了带有 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');
});

哪个将保留发送方法.

作为参考,307 http 代码规范是:

For reference, the 307 http code spec is:

307 Temporary Redirect (自 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.英国/www/post-redirect.html

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

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

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