[英]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. 我已经编写了一个node.js服务器,该服务器创建一个服务器并在使用异步功能完成后打印输出。 While I am able to get the correct output always in the console.log.
虽然我始终可以在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. 现在启动服务器并在浏览器中使用参数link和position作为get访问它,在第二次或第三次刷新时我将获得输出。 I am getting perfect output in console.log though !
我在console.log中得到了完美的输出!
Can anyone help or guide me in this regard ? 有人可以在这方面帮助或指导我吗?
Thanks ! 谢谢 !
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. 并且由于您的
globalImage
是全局变量,因此一旦加载,它就会保留在内存中,这就是为什么您需要尝试才能获取数据的原因。
Just move your res.write and res.end in the callback function, this way the content will be sent once the image is loaded. 只需在回调函数中移动您的res.write和res.end,这样在加载图像后便会发送内容。
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. 无论如何,除非您要缓存图像,否则您不应该具有
globalImage
变量,因为即使您希望对其进行垃圾回收,它也将保留在内存中。 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");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.