I trying to have a loop with http.get requests - and I don't know why the function doesn't starts. my code is :
while(List.length>0) {
if(counter < Limit) { // Limit is the amount of requests I want to
counter++;
inProcess++;
var scanitem = List.pop();
var body = "";
var url = 'https://www.mywebsite.com/'+scanitem;
var options = {
path: url,
method: 'GET'
};
console.log(url); // This part is happenning
var _request = https.get(options, function (res) {
console.log('get web site');// this part is NOT showup.
res.on('data', function (chunk) {
console.log(chunk); // this part is NOT showup.
body += chunk.toString();
});
res.on("end", function() {
console.log(body);// this part is NOT showup.
});
res.on("error", function(error) {
console.log('error')// this part is NOT showup.
});
});
_request.end();
}
else {
console.log('list BREAK');} // This part is happenning after the limit crossed
When passing an Object as the 1st argument, the URL should be broken up into individual pieces:
var options = {
method: 'GET',
protocol: 'https:',
hostname: 'www.mywebsite.com',
path: '/' + scanitem
};
var _request = https.get(options, ...);
The options that are used are covered under https.request(), which https.get() is a convenience variant of.
You can also pass the URL String, which https.get() will be run through url.parse() for you:
var _request = https.get(url, ...);
JavaScript doesn't have block-scoped variables (yet). So, despite the location of var body = "";, every iteration of your while loop is still appending to the same body.
This isn't as much a concern when the variable is only used by synchronous tasks, like scanitem, url, and options are. But, when mixing in asynchronous tasks like https.get(), you won't likely get the result you're expecting.
In the current absence of block-scoped variables, you can use a closure to create an additional function scope.
As List appears to be an Array, you can use an iterator function with .forEach() for this:
List.forEach(function (scanitem) {
var body = '';
var url = 'https://www.mywebsite.com/'+scanitem;
https.get(url, function (res) {
// etc.
});
});
And, for the Limit, you can use .splice() to remove and work with the portion of the Array you want:
List.splice(0, Limit).forEach(function (scanitem) {
// etc.
});
Also, unlike with https.request(), you're not required to call _request.end() when using https.get().