0

I am new in node.js and javascript. I want to scraping the website and handle the html sources. so I found the way to tracking way, Like this.

var request = require('request');
var aaaa =""; 
request('http://www.google.com', function (error, response, body) {
    if (!error && response.statusCode == 200) {
    aaaa =  body;
    }
});
console.log(aaaa);

I can show the google's html source, like this

request('http://www.google.com', function (error, response, body) {
    if (!error && response.statusCode == 200) {
    console.log(body);
    }
});

but to do so, I can't handle the google's html source any more, so my intention is getting out the the body as aaaa .

How can I make it? other way to making same purpose?

  • The `request()` function is asynchronous which means its callback happens sometime later, long after the main function has already returned. The only place you can handle the response is inside the callback itself. Put your code there. This is a dup of many - I will look for a good reference duplicate question. – jfriend00 Jun 07 '15 at 16:10
  • http://blog.slaks.net/2015-01-04/async-method-patterns/ – SLaks Jun 07 '15 at 16:10
  • Thank you, jfriend000. I didn't know it is asynchronous. I put together that one, I found what you mean. I should put that function on callback. – Marcux Shinee Jun 07 '15 at 16:28
  • @MarcuxShinee just a tip you can type var aaaa; you don't need to do var aaaa=""; – Datsik Jun 07 '15 at 16:59

0 Answers0