Node.js global proxy setting

前端 未结 5 2265
孤城傲影
孤城傲影 2020-12-12 16:58

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 mo

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 17:28

    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();
    

提交回复
热议问题