Node.js 如何反向代理客户端POST&;使用节点http代理放置请求
我试图使用节点http代理作为反向代理,但我似乎无法获得POST并将请求投入工作。文件server1.js是反向代理(至少对于url为“/转发此”的请求),server2.js是接收代理请求的服务器。请解释我做错了什么 以下是server1.js的代码:Node.js 如何反向代理客户端POST&;使用节点http代理放置请求,node.js,node-http-proxy,Node.js,Node Http Proxy,我试图使用节点http代理作为反向代理,但我似乎无法获得POST并将请求投入工作。文件server1.js是反向代理(至少对于url为“/转发此”的请求),server2.js是接收代理请求的服务器。请解释我做错了什么 以下是server1.js的代码: // File: server1.js // var http = require('http'); var httpProxy = require('http-proxy'); httpProxy.createServer(functio
// 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();
}
}
// 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();
}
下面是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代理依赖于POST/PUT请求的
数据
和结束
事件。server1接收请求和代理请求之间的延迟意味着http代理完全错过这些事件。这里有两个选项可以让它正常工作-您可以使用,也可以使用。在这里,路由代理似乎最合适,因为您只需要代理请求的子集。以下是修订后的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();
}
}
// 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 “上面的代码适用于http proxy~0.10.x。从那时起,库中的许多内容都发生了更改。下面是新版本的示例(在编写~1.0.2时)”
这是我的代理POST请求的解决方案。这不是最理想的解决方案,但它很有效,而且很容易理解
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)
});
}
端口
10500
和9000
是任意的,在我的代码中,我根据我托管的服务动态分配它们。这不处理PUT,而且可能效率较低,因为我正在创建另一个响应,而不是操纵当前响应 尽管这是一个旧的答案,但要使其正常工作,您应该替换var proxy=new-httpProxy.RoutingProxy()代码>与var proxy=httpProxy.createProxyServer({})代码>