如何将 http 代理与 node.js http.Client 一起使用? [英] How can I use an http proxy with node.js http.Client?
问题描述
I want to make an outgoing HTTP call from node.js, using the standard http.Client
. But I cannot reach the remote server directly from my network and need to go through a proxy.
How do I tell node.js to use the proxy?
Tim Macfarlane's answer was close with regards to using a HTTP proxy.
Using a HTTP proxy (for non secure requests) is very simple. You connect to the proxy and make the request normally except that the path part includes the full url and the host header is set to the host you want to connect to.
Tim was very close with his answer but he missed setting the host header properly.
var http = require("http");
var options = {
host: "proxy",
port: 8080,
path: "http://www.google.com",
headers: {
Host: "www.google.com"
}
};
http.get(options, function(res) {
console.log(res);
res.pipe(process.stdout);
});
For the record his answer does work with http://nodejs.org/ but that's because their server doesn't care the host header is incorrect.
这篇关于如何将 http 代理与 node.js http.Client 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!