Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the client IP from a request object with Restify?

I'm having a difficult time finding how to access the IP address of the REST client from a route.

server.get('api/foo', function(req, res, next) {
    // How can I access the IP address of the requester from here?
}
like image 485
Coder1 Avatar asked Jan 26 '15 20:01

Coder1


People also ask

How do I find the IP address of my client?

After the client establishes a successful connection to the server, the IP address of the client will be printed on the server console.

How do I find the client IP address from http request node?

The easiest way to find your IP address in Node. js is to pull the client IP address from the incoming HTTP request object. If you are running Express in your Node app, this is very easy to do. Access the socket field of the Express request object, and then look up the remoteAddress property of that field.

What is req connection remoteAddress?

req. connection. remoteAddress will contain the IP-address of the client making the request, which in case of a proxy server will be the proxy's IP-address and not the one from the client on whose behalf the proxy is forwarding the request.


2 Answers

This worked:

req.connection.remoteAddress

like image 107
Coder1 Avatar answered Nov 16 '22 01:11

Coder1


The other answers won't work behind a proxy, you'll get the proxy server address in those cases.

req.headers['x-forwarded-for'] || req.connection.remoteAddress;

Will work behind a proxy if the proxy sets the original IP in x-forwarded-for header which many do by default and you can add to something like nginx very easily.

like image 29
Kurt Koller Avatar answered Nov 16 '22 01:11

Kurt Koller