Node.js 设置代理的两种方式:superagent-proxy 和 https-proxy-agent
Node.js 设置代理有两种方式:superagent-proxy 和 https-proxy-agent,支持 http 和 https 两种代理协议
proxy-agent 官网:https://www.npmjs.com/package/proxy-agent
superagent-proxy 官网:https://www.npmjs.com/package/superagent-proxy
http-proxy-agent 官网:https://www.npmjs.com/package/http-proxy-agent
https-proxy-agent 官网:https://www.npmjs.com/package/https-proxy-agent
socks-proxy-agent 官网:https://www.npmjs.com/package/socks-proxy-agent
安装方法
cd /usr/local/node/lib/node_modules/npm // 必须进入此目录安装,否则会报错
// sudo npm install request
sudo npm install proxy-agent
sudo npm install superagent-proxy
sudo npm install http-proxy-agent
sudo npm install https-proxy-agent
sudo npm install socks-proxy-agent
protocol |
proxy agent for http requests
|
proxy agent for https requests
|
example |
---|---|---|---|
http
|
http-proxy-agent | https-proxy-agent |
http://proxy-server-over-tcp.com:3128
|
https
|
http-proxy-agent | https-proxy-agent |
https://proxy-server-over-tls.com:3129
|
socks(v5)
|
socks-proxy-agent | socks-proxy-agent |
socks://username:password@some-socks-proxy.com:9050 (username & password are optional)
|
socks5
|
socks-proxy-agent | socks-proxy-agent |
socks5://username:password@some-socks-proxy.com:9050 (username & password are optional)
|
socks4
|
socks-proxy-agent | socks-proxy-agent |
socks4://some-socks-proxy.com:9050
|
pac
|
pac-proxy-agent | pac-proxy-agent |
pac+http://www.example.com/proxy.pac
|
示例代码
1. superagent-proxy 官方示例
vim nodejs-proxy-01.js
var request = require('superagent'); // extend with Request#proxy() require('superagent-proxy')(request); // HTTP, HTTPS, or SOCKS proxy to use var proxy_uri = process.env.http_proxy || 'http://79.137.80.210:3128'; request .get(process.argv[2] || 'https://apps.bdimg.com/libs/jquery-i18n/1.1.1/jquery.i18n.min.js') .proxy(proxy_uri) .buffer(true) .set('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36') .set('Host','apps.bdimg.com') .set('Accept','text/html,application/xhtml+xml,application/xml,application/x-javascript;q=0.9,image/webp,image/apng,*/*;q=0.8') .set('Accept-Encoding','gzip, deflate, br') .end(onresponse); function onresponse (err, res) { if (err) { console.log(err); } else { console.log(res.status, res.headers); console.log(res.body); console.log(res.text); } }
运行结果:
2. https-proxy-agent 官方示例
vim nodejs-proxy-02.js
var url = require('url'); var https = require('https'); var HttpsProxyAgent = require('https-proxy-agent'); // HTTP/HTTPS proxy to connect to var proxy = process.env.http_proxy || 'http://79.137.80.210:3128'; console.log('using proxy server %j', proxy); // HTTPS endpoint for the proxy to connect to var endpoint = process.argv[2] || 'https://apps.bdimg.com/libs/jquery-i18n/1.1.1/jquery.i18n.min.js'; console.log('attempting to GET %j', endpoint); var options = url.parse(endpoint); // create an instance of the `HttpsProxyAgent` class with the proxy server information var agent = new HttpsProxyAgent(proxy); options.agent = agent; headers = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36", "Host": "apps.bdimg.com", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", // "Accept-Encoding": "gzip, deflate, br", }; options.headers = headers; https.get(options, function (res) { console.log('"response" event!', res.headers); res.pipe(process.stdout); });
运行结果:
完整的代理示例
/** * http://proxy.mimvp.com * 2017.7.20 * * https-proxy-agent : https://www.npmjs.com/package/https-proxy-agent * superagent-proxy : https://www.npmjs.com/package/superagent-proxy */ ////////////////////////// https-proxy-agent - success ////////////////////////// var url = require('url'); var https = require('https'); var HttpsProxyAgent = require('https-proxy-agent'); // HTTP/HTTPS proxy to connect to var proxy = 'http://79.137.80.210:3128'; console.log('using proxy server %j', proxy); var endpoint = process.argv[2] || 'https://apps.bdimg.com/libs/jquery-i18n/1.1.1/jquery.i18n.min.js'; console.log('attempting to GET %j', endpoint); var options = url.parse(endpoint); // create an instance of the `HttpsProxyAgent` class with the proxy server information var agent = new HttpsProxyAgent(proxy); options.agent = agent; options.port = 443; options.secureProxy = true; headers = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36", "Host": "apps.bdimg.com", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, br", }; options.headers = headers; //console.log('options %j', options); https.get(options, function (res) { console.log('"response" event!', res.headers); res.pipe(process.stdout); }); ////////////////////////// superagent-proxy - success ////////////////////////// var superagent = require('superagent'); require('superagent-proxy')(superagent); function check(uri) { var proxy_ip = uri.toString().split(",")[0]; var proxy_type = uri.toString().split(",")[1]; var proxy_uri = "http://" + proxy_ip; // http https both to http://ip:port console.log("proxy_uri : " + proxy_uri); superagent .get('https://apps.bdimg.com/libs/jquery-i18n/1.1.1/jquery.i18n.min.js') .proxy(proxy_uri) .buffer(true) .set('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36') .set('Host','apps.bdimg.com') .set('Accept','text/html,application/xhtml+xml,application/xml,application/x-javascript;q=0.9,image/webp,image/apng,*/*;q=0.8') .set('Accept-Encoding','gzip, deflate, br') .end(function onResponse(err, res) { if (err) { console.log(err); } else { console.log(res.status, res.headers); console.log(res.text); } }); }; //check('218.241.131.249:80,HTTP'); // http check('79.137.80.210:3128,HTTPS'); // https
以上测试代理,均来自米扑代理,其网站支持http、https、socks4、socks5等多种类型代理,且代理质量非常高
米扑代理官网: http://proxy.mimvp.com
参考推荐:
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2017-10-22 22:41:49
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!
转载注明: Node.js 设置代理的两种方式:superagent-proxy 和 https-proxy-agent (米扑博客)