Node.js
- 13 ответов
- 0 вопросов
7
Вклад в тег
//Для начала установите модуль request
//npm i request --save
/**
* make get request through proxy
* @param {string} url - uri of resource
* @param {object[]} proxyList - array of proxies
* @param {string} proxyType='http' - type of proxy
* @param {function} cb - callback
*/
const testProxy = (url, proxyList, proxyType, cb) => {
if(!(proxyList instanceof Array) && !(cb instanceof Function) && !url){
throw new Error('testProxy() arguments not set');
}
proxyType = proxyType || 'http';
const proxyIndex = parseInt(Math.random() * (proxyList.length - 1));
const proxy = proxyList[proxyIndex];
const proxyUrl = `${proxyType}://${proxy.ipAddress}:${proxy.port}`;
const anyParam = {method : 'GET', proxy : proxyUrl, strictSSL: false};
request(url, anyParam, (error, response, body) => {
if(error){
cb(error);
} else {
cb(null, response, body);
}
});
};