我想在节点js中使用代理来请求html每个查询。 我找到了一个请求模块,它允许使用代理并且易于使用。
对我来说,它适用于简单的请求,但代理池 - 不是。 它引发了我的错误:
如何解决?
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);
答案 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);
});