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)
source share