Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

Node.js global proxy setting

I was working in a corporate network behind a proxy server. In my code I can set the proxy by using the approach mentioned in this thread.

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 direct 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?

question from:https://stackoverflow.com/questions/18586902/node-js-global-proxy-setting

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

var globalTunnel = require('global-tunnel-ng');

globalTunnel.initialize({
  host: '10.0.0.10',
  port: 8080,
  proxyAuth: 'userId:password', // optional authentication
  sockets: 50 // optional pool size for each http and https
});

After the global settings are establish with a call to initialize, both http.request and the request library will use the proxy information.

The module can also use the http_proxy environment variable:

process.env.http_proxy = 'http://proxy.example.com:3129';
globalTunnel.initialize();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...