Question

I have a small server script written in nodejs and I would like to get the client IP, but I don't seem to find my way yet.

I am using the Connect package to create my server.

Does anybody know how to get the ip using Connect package of nodejs.

I've also check the documentation here: http://www.senchalabs.org/connect/ but I didn't find it, may be I missed something.

Was it helpful?

Solution 2

I solved my problem using:

var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || 
    req.socket.remoteAddress || req.connection.socket.remoteAddress;

and I got this from this answer: https://stackoverflow.com/a/19524949/1358670

OTHER TIPS

You probably can't, when connected directly (not from a proxy), but try for yourself. I took the hello world example from the connect-readme and just print out the contents of the request object:

var connect = require('connect')
  , http = require('http');
  , util = require('util');

var app = connect()
  .use(function(req, res){
    console.log(util.inspect(req));
    res.end('Hello from Connect!\n');
  });

http.createServer(app).listen(3000);

You won't find any client-ip here.

But anyway, node.js-services are not supposed to be connected directly but with a forward-proxy, load-balancer or so.

When using apache's proxypass you will find the client's originating IP address in the header, look for

req.headers['X-Forwarded-For']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top