Simple example for nodejs behind a proxy server

Here is a simple nodejs script for using node as an https client to interact with the outside world. How do you change it so as not to get "ECONNREFUSED" errors when starting up with a corporate proxy, for example, http://10.10.10.10:8080 ?

 var https = require('https'); var options = { hostname: 'encrypted.google.com', port: 443, path: '/', method: 'GET' }; var req = https.request(options, function(res) { console.log("statusCode: ", res.statusCode); console.log("headers: ", res.headers); res.on('data', function(d) { process.stdout.write(d); }); }); req.end(); req.on('error', function(e) { console.error(e); }); 

---- Update ---

I found out how with the "npm" request package ( https://github.com/mikeal/request ). Still wondering how to do this with node vanilla libraries.

 var request = require('request').defaults({proxy:'http://my.proxy.server:8080', agent:false}); request('https://www.google.com', function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Print the google web page. } }) 
+4
source share
1 answer

It is usually best to handle proxy at the system level - configure your operating system to use a proxy server, not a script. However, this can also fail, since most corporate proxies do not have open ports.

A universal solution would be to have a server (EC2 or Rackspace instance) to act as your own proxy server, and make it listen to ssh on port 443. Port 443 is open on corporate https firewalls.

At this point, you can either tunnel all your network traffic to the server, or ssh to the server and run the script there.

If you were to create a proxy server specific to them, they would not be very portable, but not simplified, because they broke when changing the proxy configuration.

+1
source

All Articles