Get IP from the computer that is accessing application via Node.js

Asked

Viewed 721 times

3

I have an application . Net where for each access is recorded log with IP of the access machine, time etc.

No. Net I use Request.UserHostAddress in the login POST to pick up the IP of the machine that is accessing the application(the same local network wheel so that’s enough).

Now I’m migrating the application to Node and the doubt has arisen, there is an equivalent to Request.UserHostAddress in Ode (process in the backend the information of the machine that is accessing the application) or it will be necessary to obtain the information from the frontend?

  • Are you using a library, like Express? Or just Node.js?

  • @Luizfelipe I’m using the express right now.

  • Have you tried req.ip()?

1 answer

0


You should consider some scenarios to recover IP:

var remoteIp = (req.headers['x-forwarded-for'] || '').split(',').pop() || // Recupera o IP de origem, caso a fonte esteja utilizando proxy
                 req.connection.remoteAddress || // Recupera o endereço remoto da chamada
                 req.socket.remoteAddress || // Recupera o endereço através do socket TCP
                 req.connection.socket.remoteAddress // Recupera o endereço através do socket da conexão

With this you guarantee to recover a remote address, whether it is using a proxy or not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.