Prashant Singh Prashant Singh - 4 months ago 8
Node.js Question

Node js working on second or third call

I have written a node.js server which creates a server and prints the output when done with an asynchronous function. While I am able to get the correct output always in the console.log. The same is not getting reflected in my response. Here is my code snippet :-

var request = require('request');
var http = require('http');
var cheerio = require('cheerio');
var url = require('url');
var Curl = require( 'node-libcurl' ).Curl;
var sleep = require('sleep');
isDone = 0;
globalImage = "";

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var url_parts = url.parse(req.url, true);
var query = url_parts.query;
var main_url = query["link"];
if (req.url != '/favicon.ico') {
res.writeHead(200);

if(main_url != undefined ){
var position = parseInt(query["position"]);
// web_scrap()
web_scrap(main_url,position, function(image) {

console.log("Console log : " + image);
globalImage = image;

});
res.write(globalImage);
res.end("HEY");
}
}
else {//For favicon and other requests just write 404s
res.writeHead(404);
res.write('This URL does nothing interesting');
res.end();
}
}).listen(3000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:3000/');


function web_scrap(url, position, callback){
// do something
callback(JSON.stringify(product));
}


Now on starting the server and accessing it in browser with parameters link and position as get, I am getting output on second or third refresh. I am getting perfect output in console.log though !

Can anyone help or guide me in this regard ?

Thanks !

Answer

From what I understand, you're loading an image from an external source, asynchronously.

Thus, your function continues to run, even though the load is not finished yet. And as your globalImage is a global variable, once it is loaded, it stays in memory, that's why you get the data after some tries.

Just move your res.write and res.end in the callback function, this way the content will be sent once the image is loaded.

web_scrap(main_url,position, function(image) {
  console.log("Console log : " + image); 
  globalImage = image;
  res.write(globalImage);
  res.end("HEY");
});

Anyway, except if you want to cache your image, you should not have a globalImage variable, as it would stay in memory even though you would want it to be garbage collected. You can remove the variable and just make this:

web_scrap(main_url,position, function(image) {
  console.log("Console log : " + image); 
  res.write(image);
  res.end("HEY");
});
Comments