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

Using Android Volley to post an array to PHP -

python - How to add an model instance to a django queryset? -

angularjs - No 'Access-Control-Allow-Origin' error from local domain to public API -