This agent rotates requests trought especified proxies
If 2 proxies are specified First request goes trought first proxy Second request goes trought seconde proxy Third request goes trought first proxy
const request = require('request'); const ProxyRotator = require('proxy-rotator-agent') const agent = new ProxyRotator({ proxies: [ { proxy: { host: '10.0.0.1', port: 3128, proxyAuth: 'pass', headers: { 'user-agent': 'nautilus' }}}, { proxy: { host: '10.0.0.2', port: 4545, proxyAuth: 'proxy2pass', headers: { 'user-agent': 'firefox' }}} ] }) function optionalCallback(err, httpResponse, body) { if(err) return console.error(err); console.log(err, body); } const options = { uri: 'https://httpbin.org/ip', json: true, agent, }; request( options, optionalCallback ) request( options, optionalCallback )
Output
{ ip: '10.0.0.1' } { ip: '10.0.0.2' }
const https = require('https'); const ProxyRotator = require('proxy-rotator-agent') const agent = new ProxyRotator({ proxies: [ { proxy: { host: '10.0.0.1', port: 3128, proxyAuth: 'pass', headers: { 'user-agent': 'nautilus' }}}, { proxy: { host: '10.0.0.2', port: 4545, proxyAuth: 'proxy2pass', headers: { 'user-agent': 'firefox' }}} ] }) const options = { hostname: 'httpbin.org', port: 443, path: '/ip', agent, }; // Make a request function request(options) { const req = https.request(options); req.on('response', (res) => { res.on('data', (data) => { console.log(data.toString()) }); }); req.on('error', (err) => { console.error(err); }); req.end(); } request(options) request(options)
Output:
{
"origin": "10.0.0.1"
}
{
"origin": "10.0.0.2"
}