Node.js全局代理设置
我在代理服务器后面的裁剪networking中工作。 在我的代码中,我可以通过使用在这个线程中提到的方法来设置代理( 我如何使用http代理与node.js http.Client? )。
但问题是,大多数第三方模块没有代理设置,我不能修改他们的代码添加代理。 此外,我的代码可能会在直接连接环境中使用,这意味着我无法在代码中对代理设置进行硬编码。
我知道NPM有一个代理服务器的全局设置
npm config set proxy http://proxy.company.com:8080 npm config set https-proxy http://proxy.company.com:8080
但是我没有在Node.js中find任何类似的configuration。
Node.js是否支持全局代理设置,以便我不需要更改所有代码并轻松打开和closures。
不幸的是,似乎必须在每次调用http.request
设置代理信息。 节点不包括全局代理设置的机制。
NPM上的global-tunnel
模块似乎可以处理这个问题,但是:
var globalTunnel = require('global-tunnel'); globalTunnel.initialize({ host: 'proxy.example.com', port: 8080 });
在通过initialize
调用build立全局设置之后, http.request
和requests
库都将使用代理信息。
该模块也可以使用http_proxy
环境variables:
process.env.http_proxy = 'http://proxy.example.com:3129'; globalTunnel.initialize();
我终于创build了一个模块来解决这个问题(部分)。 基本上这个模块改写http.request
函数,添加代理设置然后启动。 检查我的博客文章http://blog.shaunxu.me/archive/2013/09/05/semi-global-proxy-setting-for-node.js.aspx
虽然不是一个Nodejs设置,我build议你使用我觉得很方便的代理链 。 它可能在您的包pipe理器中可用。
在configuration文件( /etc/proxychains.conf
)中设置代理后,可以运行proxychains npm start
或proxychains4 npm start
( proxychains [command_to_proxy_transparently]
),所有的请求都会自动代理。
为我configuration设置:
这些是您必须添加的最小设置
## 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
(你可以通过使用nslookup [proxyurl]
来获得代理的ip)