node.jsnode.js-connectclientip

Get client IP with Connect Package of connect


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.


Solution

  • 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