I'm using Request, Cheerio and Node.js
I need to limit concurrent requests to 10. I want to process URL's in parallel but not in the same time, hope it's clear.
Here's my current script. If I run my for loop from 0 to 100, it works. 100 to 200, it works. 200 to 300, it works. But 0-1000 gives connection error. I need to put a limit so it can work with 5 or 10 requests at a time.
app.get('/back', function (req, res) {
for (var y = 0; y < 1000; y++) {
(function () {
const id = y;
var url = "http://www.example.com/" + y;
var options2 = {
url: url,
headers: {
'User-Agent': req.headers['user-agent'],
'Content-Type': 'application/json; charset=utf-8'
}
};
request(options2, function (err, resp, body) {
if (err) {
console.log(err);
} else {
if ($ = cheerio.load(body)) {
var links = $('#container');
var name = links.find('span[itemprop="name"]').html(); // name
if (name == null) {
console.log("null returned");
} else {
name = entities.decodeHTML(name);
// check name existence
console.log("yay!" + name);
}
});
}
}
else {
console.log("couldn't open it");
}
}
}
);
}());
}
});