Hi All, I am fairly new to the Node.JS and Javascript for that matter. Something strange going on in the below code
The below page/code is requested by two different clients. Its all good for the first request as it logs correct IP of the requested client correctly every where. but for the second request, the IP address(reqHostIp) is correct outside the CLOSURE but inside the CLOSURE BLOCK the value of the IP address is always of the first request's I am suspecting this is to do with variable scoping or something of that sort or is it something else? Do you find anything obviously wrong in the code router.get('/', function(req, res) { utils.dump("viewer::get - " + __filename); //var reqHostIp = req.connection.remoteAddress || req.headers['x-forwarded-for']; dbutils.fetchSomeDetals(function(config) { //Some code //Some code res.render('viewer', { //Rendering code }); //let reqHostIp = req.ip; var reqHostIp = req.connection.remoteAddress || req.headers['x-forwarded-for']; utils.dump("reqHostIp: " + reqHostIp); //Correct IP logged //***********CLOSURE*********** (function(reqHostIp) { someClass.someFunction(id, ip, metric, function(err, file_name) { if (err) { utils.dump("viewer::get - comms error: " + err + ", failed"); return; } let src = '/tmp/' + file_name; utils.dump("reqHostIp: " + reqHostIp); // Wrong! Always displays the previous request's IP }); })( reqHostIp); //**************END CLOSURE******************* utils.dump("reqHostIp: " + reqHostIp); // Correct IP logged }); }); TIA -- Job board: http://jobs.nodejs.org/ New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscr...@googlegroups.com. To post to this group, send email to nodejs@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/2154584f-db49-48dc-a07b-e1378fd27f07%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.