如何使用node-http-proxy反转代理客户端POST&PUT请求

我试图使用节点HTTP代理作为反向代理,但我似乎无法获得POST和PUT请求工作。 文件server1.js是反向代理(至less对于具有url“/ forward-this”的请求),server2.js是接收代理请求的服务器。 请解释我做错了什么。

以下是server1.js的代码:

// File: server1.js // var http = require('http'); var httpProxy = require('http-proxy'); httpProxy.createServer(function (req, res, proxy) { if (req.method == 'POST' || req.method == 'PUT') { req.body = ''; req.addListener('data', function(chunk) { req.body += chunk; }); req.addListener('end', function() { processRequest(req, res, proxy); }); } else { processRequest(req, res, proxy); } }).listen(8080); function processRequest(req, res, proxy) { if (req.url == '/forward-this') { console.log(req.method + ": " + req.url + "=> I'm going to forward this."); proxy.proxyRequest(req, res, { host: 'localhost', port: 8855 }); } else { console.log(req.method + ": " + req.url + "=> I'm handling this."); res.writeHead(200, { "Content-Type": "text/plain" }); res.write("Server #1 responding to " + req.method + ": " + req.url + "\n"); res.end(); } } 

这里是server2.js的代码:

 // File: server2.js // var http = require('http'); http.createServer(function (req, res, proxy) { if (req.method == 'POST' || req.method == 'PUT') { req.body = ''; req.addListener('data', function(chunk) { req.body += chunk; }); req.addListener('end', function() { processRequest(req, res); }); } else { processRequest(req, res); } }).listen(8855); function processRequest(req, res) { console.log(req.method + ": " + req.url + "=> I'm handling this."); res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write("Server #2 responding to " + req.method + ': url=' + req.url + '\n'); res.end(); } 

http-proxy取决于POST / PUT请求的dataend事件。 server1收到请求的时间与代理的时间之间的延迟意味着http-proxy完全忽略了这些事件。 在这里有两个选项可以使其正确工作 – 您可以缓冲请求 ,也可以使用路由代理 。 路由代理似乎是最合适的,因为你只需要代理一部分请求。 这里是修改的server1.js:

 // File: server1.js // var http = require('http'); var httpProxy = require('http-proxy'); var proxy = new httpProxy.RoutingProxy(); http.createServer(function (req, res) { if (req.url == '/forward-this') { return proxy.proxyRequest(req, res, { host: 'localhost', port: 8855 }); } if (req.method == 'POST' || req.method == 'PUT') { req.body = ''; req.addListener('data', function(chunk) { req.body += chunk; }); req.addListener('end', function() { processRequest(req, res); }); } else { processRequest(req, res); } }).listen(8080); function processRequest(req, res) { console.log(req.method + ": " + req.url + "=> I'm handling this."); res.writeHead(200, { "Content-Type": "text/plain" }); res.write("Server #1 responding to " + req.method + ": " + req.url + "\n"); res.end(); } 

除了@squamos

如何编写一个节点快递应用程序,服务大多数本地文件,但重新路由到另一个域?

 var proxy = new httpProxy.RoutingProxy(); 

“上面的代码是为http-proxy〜0.10.x工作的,从那以后,很多东西在库中已经改变了,下面你可以find新版本的例子(在编写时〜1.0.2)”

 var proxy = httpProxy.createProxyServer({}); 

这是我的代理POST请求的解决scheme。 这不是最理想的解决scheme,但工作起来很容易理解。

 var request = require('request'); var http = require('http'), httpProxy = require('http-proxy'), proxy = httpProxy.createProxyServer({}); http.createServer(function(req, res) { if (req.method == 'POST') { request.post('http://localhost:10500/MyPostRoute', {form: {}}, function(err, response, body) { if (! err && res.statusCode == 200) { // Notice I use "res" not "response" for returning response res.writeHead(200, {'Content-Type': "application/json"}); res.end(body); } else { res.writeHead(404, {'Content-Type': "application/json"}); res.end(JSON.stringify({'Error': err})); } }); } else if (req.method == 'GET') { proxy.web(req, res, { target: 'http://localhost/9000' }, function(err) { console.log(err) }); } 

端口105009000是任意的,在我的代码中,我根据我托pipe的服务dynamic分配它们。 这不涉及PUT,可能效率较低,因为我正在创build另一个响应,而不是操作当前的响应。