ExpressJS:如何使用参数重定向 POST 请求 [英] ExpressJS : How to redirect a POST request with parameters

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

问题描述

我需要将 node.js 服务器的所有 POST 请求重定向到远程服务器.

我尝试执行以下操作:

app.post('^*$', function(req, res) {res.redirect('http://remoteserver.com' + req.path);});

重定向有效,但没有 POST 参数.我应该修改什么来保留 POST 参数?

解决方案

在 HTTP 1.1 中,有一个状态码 (307),表示应该使用相同的方法和发布数据重复请求.

<块引用>

307 Temporary Redirect (自 HTTP/1.1) 在这种情况下,应该使用另一个 URI 重复请求,但未来的请求仍然可以使用原始 URI.与303相反,重新发出原始请求时不应更改请求方法.例如,必须使用另一个 POST 请求重复 POST 请求.

在 express.js 中,状态码是第一个参数:

res.redirect(307, 'http://remoteserver.com' + req.path);

程序员堆栈交换上阅读更多相关信息.>

代理

如果这不起作用,您还可以代表用户从服务器向另一台服务器发出 POST 请求.但请注意,发出请求的是您的服务器,而不是用户.您实际上将代理请求.

var request = require('request');//npm 安装请求app.post('^*$', function(req, res) {request({ url: 'http://remoteserver.com' + req.path, headers: req.headers, body: req.body }, function(err, remoteResponse, remoteBody) {if (err) { return res.status(500).end('Error');}res.writeHead(...);//从 remoteResponse 中复制所有标头res.end(remoteBody);});});

普通重定向:

用户 ->服务器:获取/服务器->用户:位置:http://remote/用户 ->远程:获取/远程 ->用户:200 OK

发布重定向":

用户 ->服务器: POST/服务器->远程:POST/远程 ->服务器:200 OK服务器->用户:200 OK

I need to redirect all the POST requests of my node.js server to a remote server.

I tried doing the following:

app.post('^*$', function(req, res) {
  res.redirect('http://remoteserver.com' + req.path);
});

The redirection works but without the POST parameters. What should I modify to keep the POST parameters?

解决方案

In HTTP 1.1, there is a status code (307) which indicates that the request should be repeated using the same method and post data.

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. 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.

In express.js, the status code is the first parameter:

res.redirect(307, 'http://remoteserver.com' + req.path);

Read more about it on the programmers stackexchange.

Proxying

If that doesn't work, you can also make POST requests on behalf of the user from the server to another server. But note that that it will be your server that will be making the requests, not the user. You will be in essence proxying the request.

var request = require('request'); // npm install request

app.post('^*$', function(req, res) {
    request({ url: 'http://remoteserver.com' + req.path, headers: req.headers, body: req.body }, function(err, remoteResponse, remoteBody) {
        if (err) { return res.status(500).end('Error'); }
        res.writeHead(...); // copy all headers from remoteResponse
        res.end(remoteBody);
    });
});

Normal redirect:

user -> server: GET /
server -> user: Location: http://remote/
user -> remote: GET /
remote -> user: 200 OK

Post "redirect":

user -> server: POST /
server -> remote: POST /
remote -> server: 200 OK
server -> user: 200 OK

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

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