如何在节点js中使用代理池?

时间:2017-01-27 12:16:13

标签: javascript node.js proxy

我想在节点js中使用代理来请求html每个查询。 我找到了一个请求模块,它允许使用代理并且易于使用。

对我来说,它适用于简单的请求,但代理池 - 不是。 它引发了我的错误:

  • 无法建立隧道套接字,因为= connect ECONNREFUSED
  • 无法建立隧道套接字,statusCode = 400
  • 无法建立隧道套接字,因为=连接EHOSTUNREACH 0.0.13.64:80 - 本地(192.168.1.174:54491)

如何解决?

var Promise = require('bluebird');

// Rounting
var koa = require('koa');
var router = require('koa-router')();
var koaRestMongoose = require('koa-rest-mongoose');
var cors = require('koa-cors');

var mongoUrl = '127.0.0.1:27017/db';
var mongoose = require('mongoose');
    mongoose.connect(mongoUrl);

// PROXY
// This insert proxy list to the db/proxy
var proxy = require("./proxy")(mongoUrl, 'proxy');

var jsProxyPool = require('proxy-pool');
var mongoDbDataAccess = new jsProxyPool.MongoAccess(mongoUrl, 'proxy');
var poolConfig = new jsProxyPool.ProxyPoolConfiguration(mongoDbDataAccess, 20*1000);
var pool = new jsProxyPool.ProxyPool(poolConfig);

// Requesting
var request = require('request');

router.get('/url/:url', function* () {
  var promise = new Promise((resolve, reject) => {

    pool.getProxy((err, proxy) => {

      var proxy = proxy;
      var url = 'https://google.com/';

      request({
        url: url,
        proxy: {
            host: proxy._address,
            port: proxy._port,
        }
      }, (error, response, body) => {
          if (!error && response.statusCode == 200) {
              resolve(body);
          } else {
              resolve('Proxy: '+proxy._address+':'+proxy._port + '\n' + error);
          }
      });
    });
  });
  this.body = yield promise;
});

var app = koa();
app.use(cors());
app.use(router.routes());
app.listen(8005);

1 个答案:

答案 0 :(得分:0)

我添加了代理,它解决了我的问题+代理应该还活着。

        var HttpsProxyAgent = require('https-proxy-agent'); 
        var proxy = {
            host: '111.111.111.111',
            port: 1111
        };

        var agent = new HttpsProxyAgent(proxy); 

        request({  
            url: url,
            method: 'GET',
            agent: agent,
            timeout: 10000,
            followRedirect: true,
            maxRedirects: 10,
        }, (error, response, body) => {
            if (error) {
                resolve(error);
            }
            resolve(body);
        });