I was working in a crop network behind a proxy server. In my code I can set the proxy by using the approach mentioned in this thread (How can I use an http proxy with node.js http.Client?).
But the problem is that, most of the 3rd party modules do not have proxy setting and I cannot modify their code to add the proxy. Also my code might be used in a directly connection environment which means I cannot hard-code my proxy setting in code.
I know NPM has a global setting for proxy which is
npm config set proxy http://proxy.company.com:8080 npm config set https-proxy http://proxy.company.com:8080
But I didn't find any config similar in Node.js.
Does Node.js support global proxy setting so that I don't need to change all codes and switch on and off easily.
Unfortunately, it seems that proxy information must be set on each call to
http.request
. Node does not include a mechanism for global proxy settings.The
global-tunnel-ng
module on NPM appears to handle this, however:After the global settings are establish with a call to
initialize
, bothhttp.request
and therequests
library will use the proxy information.The module can also use the
http_proxy
environment variable:I finally created a module to get this question (partially) resolved. Basically this module rewrites
http.request
function, added the proxy setting then fire. Check my blog post: https://web.archive.org/web/20160110023732/http://blog.shaunxu.me:80/archive/2013/09/05/semi-global-proxy-setting-for-node.js.aspxWhile not a Nodejs setting, I suggest you use proxychains which I find rather convenient. It is probably available in your package manager.
After setting the proxy in the config file (
/etc/proxychains.conf
for me), you can runproxychains npm start
orproxychains4 npm start
(i.e.proxychains [command_to_proxy_transparently]
) and all your requests will be proxied automatically.Config settings for me:
These are the minimal settings you will have to append
(You can get the ip of the proxy by using
nslookup [proxyurl]
)