Haloor Haloor - 4 months ago 9
Node.js Question

How to collect the content of URLs in a specified sequence using node.js

I would like to collect the content from 3 different URLs, and print it to stdout. The content must be printed in the sequence that the URLs are supplied i.e. process.argv[2] = URL1, process.argv[3] = URL2 etc.. I thought that if I used a series of nested callbacks this would work but instead what is happening is that some of the content of the 2nd URL starts to get printed before the 1st URL (it varies each time depending on how long it takes to load the page, but it is never synchronous) - How to code this so that the 2nd URL is only printed after the 1st URL is completely finished etc... Here is my code:

var http = require('http');

var webpage1 = process.argv[2];
var webpage2 = process.argv[3];
var webpage3 = process.argv[4];

container(callback1, callback2, callback3);

function container(callback1, callback2, callback3) {
callback1(callback2, callback3);
}

function callback1(callback2, callback3) {
http.get(webpage1, function (response) {
var body1 = [];
response.on('data', function (chunk) {
body1.push(chunk);
}).on('end', function () {
body1 = Buffer.concat(body1).toString();
console.log(body1);
});
});
callback2(callback3);
}

function callback2(callback3) {
http.get(webpage2, function cb(response) {
var body2 = [];
response.on('data', function (chunk) {
body2.push(chunk);
}).on('end', function () {
body2 = Buffer.concat(body2).toString();
console.log(body2);
});
});
callback3();
}

function callback3() {
http.get(webpage3, function cb(response) {
var body3 = [];
response.on('data', function (chunk) {
body3.push(chunk);
}).on('end', function () {
body3 = Buffer.concat(body3).toString();
console.log(body3);
});
});
}

Answer

You don't need to write same code 3 times. Also, no need to use callbacks here.

var http = require('http');
var numOfURLs = 3;

(function printURL(a){
    http.get(process.argv[a + 2], function (response) {
      var body = [];
      response.on('data', function (chunk) {
          body.push(chunk);
      }).on('end', function () {
          body = Buffer.concat(body).toString();
          console.log(body);
          if(a < numOfURLs - 1) printURL(a + 1);
      });
  });
})(0);