Reputation: 247
I have a node server and based on some recent infrastructure changes, I need to make sure all outbound requests are going through a Squid proxy EXCEPT for traffic to hosts listed in the NO_PROXY environment variable.
Setting HTTP_PROXY, HTTPS_PROXY and NO_PROXY doesn't seem to affect the node server's behavior, but I need a way to do this without having to manually edit libraries. I have ~10 libraries that reach out to external services that need to respect this proxying behavior.
Is there some other way I can globally set proxying behavior including respecting NO_PROXY?
Upvotes: 9
Views: 22379
Reputation: 4830
Unfortunately, the Node.js runtime does not support configuring HTTP proxying using environment variables out of the box (see nodejs/node#8381 and nodejs/node#15620). Request library support is mixed:
request
does (request/request#1096)node-fetch
does not (node-fetch/node-fetch#195) (see below)got
does not (sindresorhus/got#79) (see below)Any library which provides a way to pass an http.Agent
which can support environmentally-configured proxies, such as:
For example:
import fetch from 'node-fetch';
import ProxyAgent from 'proxy-agent';
const response = await fetch('https://example.com', { agent: new ProxyAgent() });
const body = await response.text();
If you can not pass an Agent
, or can't modify the code at all, you may want to consider global-agent
which can be used as a pre-loaded module to proxy all requests (configured by the GLOBAL_AGENT_HTTP_PROXY
environment variable):
export GLOBAL_AGENT_HTTP_PROXY=http://proxy.example.com
node --require global-agent/bootstrap mycode.js
Alternatively, if your system supports LD_PRELOAD
or DYLD_INSERT_LIBRARIES
, you could use a general-purpose proxifier such as ProxyChains-NG (or the original ProxyChains).
Upvotes: 15
Reputation: 9
Just in case someone arrives here, it seems it was already added to request.js in https://github.com/request/request/pull/1096
Upvotes: 0