0

I have a node.js file call test.js below. Don't know why the 'All done!' will always print out on the top of output. Can someone explain why the 'All done!' always be the first output in console. How can I print it as last line after all requests finished and exit the application? I tried to use setTimeout to make the 'All done' be the last, but it won't help since you never know how long each request will complete. Should I use process.exit()?

var async = require('async');
var request = require('request');
var http = require('http');

var websites = [
    'qz.com',
    'www.stackoverflow.com',
    'www.google.com'
];

//get header of each URL
var getHeader = function(url, callback){
  var options = {
    method: 'HEAD',
    host: url, 
    port: 80,
    timeout: 10000,
    followRedirect: true,
    maxRedirects: 10, 
    path: '/'
  };

  var req = http.request(options, function(res) {
    //If connect server successfully, get Status code and header
    console.log('website name:' + url);
    console.log('Status Code:' + res.statusCode);
    console.log(JSON.stringify(res.headers));
    console.log("\n");
  });

  //if Error happens print out error
  req.on('error', function(err) {
      console.log('website name:' + url);
    console.log(err);
    console.log("\n");
  });

  req.end();
  return callback(null);
}


async.each(websites, getHeader, function(err){
  // if error happens, remind user
    if(err) {
    console.log('one url failed to be processed.');
  } else {
    console.log('All done!');  //print all done when all request complete
  }

});

1 Answer 1

1

You're calling the callback immediately at the end of your getHeader function. You need to instead call it when the request completes:

var getHeader = function(url, callback){
  var options = {
    method: 'HEAD',
    host: url, 
    port: 80,
    timeout: 10000,
    followRedirect: true,
    maxRedirects: 10, 
    path: '/'
  };

  var req = http.request(options, function(res) {
    //If connect server successfully, get Status code and header
    console.log('website name:' + url);
    console.log('Status Code:' + res.statusCode);
    console.log(JSON.stringify(res.headers));
    console.log("\n");
    res.resume(); // flush response stream just in case
    callback();
  });

  //if Error happens print out error
  req.on('error', function(err) {
    console.log('website name:' + url);
    console.log(err);
    console.log("\n");
    callback(err);
  });

  req.end();
}

Also, your options contains settings not available for http.request. They are for a third-party request module.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Mscdex, I removed "return callback(null); ", but it won't print out the "All done". In fact, the whole app hook even though all requests completed. I understand I should call the callback in getHeader until request complete, but don't where I should put that logic. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.