Friday, 7 April 2017

Node JS: How do I request multiple source codes from different websites?

I get an issue where If I try to use request more than once, it doesn't work. I can give an example:

request('https://www.google.com', function (error, response, vBody1) {
    request('https://www.purple.com', function (error, response, vBody2) {
        request('https://www.stackoverflow.com', function (error, response, vBody3) {
            console.log(vBody3);
        });
        console.log(vBody2);
    });
    console.log(vBody1);
});

It reads and outputs the body of the first site, but not the next two. Here is another example:

vSites = ['https://www.google.com','https://www.purple.com','https://www.stackoverflow.com'];
for (i = 0; i < vSites.length; i++){
    request(vSites[i], function (error, response, vBody[i]) {
        console.log(vBody[i]);
    });
}

This one looks cleaner and I would like this version, but it doesn't work at all. What am I doing wrong?



via TheBloodSeeker005

No comments:

Post a Comment