Using node behind firewall with HTTP_PROXY

1,277 views
Skip to first unread message

Tauren Mills

unread,
Jan 30, 2012, 2:49:23 PM1/30/12
to nod...@googlegroups.com
Is there a way to get ALL outbound HTTP requests from node apps to use
the HTTP_PROXY environment variable? I'm behind a corporate firewall
and must go through a proxy.

In my own code, I can successfully get through the proxy by using
Mikeal's request:

var request = require('request');
request = request.defaults({proxy: process.env.HTTP_PROXY });
request.get({
url: "http://google.com"
}, function(e,r,body) {
if (body.indexOf('doctype')) {
console.log("Loaded google.com");
}
else {
console.log("Error loading google.com", e);
}
});

But what about 3rd-party libs installed via NPM? Is there a way to get
them to recognize the HTTP_PROXY setting? Obviously, I can't hack all
of them to update request.get with my proxy setting. And some of them
don't use request. As you can see above, I've even setting a default
proxy in my app logic. But as I assumed, this doesn't pass through to
other libs. For instance, I'm trying to get this working with jsdom:

var request = require('request');
request = request.defaults({proxy: process.env.HTTP_PROXY });
var jsdom = require('jsdom');
jsdom.env("http://nodejs.org/dist/", [
'http://code.jquery.com/jquery-1.5.min.js'
],
function(errors, window) {
console.log("Window: ", window);
if (errors) {
console.log("Error: ",errors);
}
else {
console.log("There have been", window.$("a").length, "nodejs
releases!");
}
});

This results in:

Window: undefined
Error occurred { [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }

There must be others building Node apps behind a proxy. How do you
deal with this?

Thanks!
Tauren

Boris Simandoff

unread,
Feb 23, 2012, 7:53:36 AM2/23/12
to nodejs
Unfortunately, I also couldn't find way to make JSDOM to use
http_proxy.
You can use "request" to download your html using proxy and submit the
body to jsdom:

request({ uri:'http://www.google.com' }, function (e, r, html) {
if (e && r.statusCode !== 200) {
console.log("Some error message");
} else {
jsdom.env({html: html, scripts: [__dirname + "/static/jq.js"]}, //
load local copy of jquery.js
function (err, window) {
var $ = window.jQuery;
console.log($('body').html());
});
}
});

Cheers,
B Simandoff
Reply all
Reply to author
Forward
0 new messages