Node.js 全局代理设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18586902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js global proxy setting
提问by Shaun Xu
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.
但问题是大多数 3rd 方模块没有代理设置,我无法修改他们的代码来添加代理。此外,我的代码可能用于直接连接环境,这意味着我无法在代码中硬编码我的代理设置。
I know NPM has a global setting for proxy which is
我知道 NPM 有一个全局代理设置,它是
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.
但是我在 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?
Node.js 是否支持全局代理设置,这样我就不需要更改所有代码并轻松打开和关闭?
回答by apsillers
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.
不幸的是,似乎必须在每次调用时设置代理信息http.request。Node 不包括全局代理设置机制。
The global-tunnel-ngmoduleon NPM appears to handle this, however:
NPM 上的global-tunnel-ng模块似乎可以处理此问题,但是:
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.requestand the requestlibrarywill use the proxy information.
通过调用 建立全局设置后initialize,库http.request和request库都将使用代理信息。
The module can also use the http_proxyenvironment variable:
该模块还可以使用http_proxy环境变量:
process.env.http_proxy = 'http://proxy.example.com:3129';
globalTunnel.initialize();
回答by Shaun Xu
I finally created a module to get this question (partially) resolved. Basically this module rewrites http.requestfunction, 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.aspx
我终于创建了一个模块来(部分)解决这个问题。基本上这个模块重写http.request功能,添加代理设置然后触发。查看我的博文:https: //web.archive.org/web/20160110023732/http: //blog.shaunxu.me: 80/archive/2013/ 09/05/semi-global-proxy-setting-for-node .js.aspx
回答by Megh Parikh
While not a Nodejs setting, I suggest you use proxychainswhich I find rather convenient. It is probably available in your package manager.
虽然不是 Nodejs 设置,但我建议您使用我觉得相当方便的代理链。它可能在您的包管理器中可用。
After setting the proxy in the config file (/etc/proxychains.conffor me), you can run proxychains npm startor proxychains4 npm start(i.e. proxychains [command_to_proxy_transparently]) and all your requests will be proxied automatically.
在配置文件(/etc/proxychains.conf对我而言)中设置代理后,您可以运行proxychains npm start或proxychains4 npm start(即proxychains [command_to_proxy_transparently])并且您的所有请求都将被自动代理。
Config settings for me:
我的配置设置:
These are the minimal settings you will have to append
这些是您必须附加的最小设置
## Exclude all localhost connections (dbs and stuff)
localnet 0.0.0.0/0.0.0.0
## Set the proxy type, ip and port here
http 10.4.20.103 8080
(You can get the ip of the proxy by using nslookup [proxyurl])
(可以通过使用获取代理的ip nslookup [proxyurl])
回答by vijay
replace {userid}and {password}with your id and password in your organization or login to your machine.
将{userid}和{password}替换为您组织中的 ID 和密码,或者登录到您的机器。
npm config set proxy http://{userid}:{password}@proxyip:8080/
npm config set https-proxy http://{userid}:{password}@proxyip:8080/
npm config set http-proxy http://{userid}:{password}@proxyip:8080/
strict-ssl=false

