Node.js - Issues with request.client._peername.address when getting Client IP -



Node.js - Issues with request.client._peername.address when getting Client IP -

i'm looking @ ways client's ip adress when making connection using http module. found 2 ways this, there may more:

request.connection.remoteaddress request.client._peername.address

(1) seems work fine, see unusual behavior when using (2). take @ next examples:

var http = require('http'); var server = http.createserver(function(request, response) { response.writehead(200, {"content-type": "text/plain"}); var clientip1 = request.connection.remoteaddress; var clientip2 = request.client._peername.address; response.write("you (1): " + clientip1 + "\n" + "you (2): " + clientip2 + "\n"); response.end(); }).listen(8080);

code above works fine. let's create changes it.

var server = http.createserver(function(request, response) { response.writehead(200, {"content-type": "text/plain"}); // removed (1). var clientip2 = request.client._peername.address; response.write("you (2): " + clientip2 + "\n"); response.end(); }).listen(8080);

the above doesn't work. how weird that? gives next error:

var clientip2 = request.client._peername.address; typeerror: cannot read property 'address' of undefined

further changes:

var server = http.createserver(function(request, response) { response.writehead(200, {"content-type": "text/plain"}); var trash = request.client._peername.address; var clientip2 = request.client._peername.address; var clientip1 = request.connection.remoteaddress; response.write("you (1): " + clientip1 + "\n" + "you (2): " + clientip2 + "\n"); response.end(); }).listen(8080);

the above work.

so seems order of import - request.client._peername not instantiated before request.connection.remoteaddress executed. in fact, can not utilize (2) if haven't used (1) beforehand.

it isn't big issue, please explain inner workings here?

node.js

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -