Question
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?
Answer
Tim Macfarlane's [answer](https://stackoverflow.com/questions/3862813/how-can-i-use-an-http- proxy-with-node-js-http-client/5810547#5810547) 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.