I am using very old version(0.15) of NodeJS and this behavior is due some kind of bug in the NodeJS and I guess the latest versions may not suffer from this. I fixed it by making the reqHostIp a global variable. But I have to upgrade the NodeJS at some point. Thanks again
On Tuesday, June 21, 2016 at 2:22:10 AM UTC+1, Ram Mulage wrote: > > Thanks for the reply, Ryan. I don't think the code is doing registering of > callbacks but I will check again. You are right, the extra wrapping of the > Closure doesn't affect the behavior in any way, I just had it to avoid any > issues related to scoping of the variables. If I remove the Closure the > behavior is still the same. > > On Friday, June 17, 2016 at 9:30:36 PM UTC+1, Ryan Graham wrote: >> >> I don't see anything obvious, but these are the questions I would be >> asking myself while investigating. >> >> Is "someClass.someFunction" taking a single use call callback? Or does >> the first call to it register the callback somewhere and then reuse it for >> future results? >> >> Your extra closure wrapping doesn't appear to be necessary, but it >> shouldn't be causing any problems. Does the behavior change if you remove >> it? >> >> Are you using a transpiler, or is this code run as-is? >> >> ~Ryan >> >> On Fri, Jun 17, 2016, 7:28 AM Ram Mulage <them...@gmail.com> wrote: >> >>> 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+un...@googlegroups.com. >>> To post to this group, send email to nod...@googlegroups.com. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/nodejs/2154584f-db49-48dc-a07b-e1378fd27f07%40googlegroups.com >>> >>> <https://groups.google.com/d/msgid/nodejs/2154584f-db49-48dc-a07b-e1378fd27f07%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> For more options, visit https://groups.google.com/d/optout. >>> >> -- >> >> ~Ryan >> > -- 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/0ade63bc-f89b-419a-ae00-668d1dd63fad%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.