0
I am working on my first professional project using Node.js. In it I make changes to security groups of EC2 AWS instances.
One of the data in which the system performs updates is the client’s IP. I am currently using the Ipify API to recover the IP. But the API is returning the server IP, and I need it to be the client IP.
The code being used in the API is like this:
const http = require('http')
http.get({ host: 'api.ipify.org', port: 80, path: '/' }, function (resp) {
resp.on('data', function (ip) {
const ipCliente = ip.toString('utf8') + '/32'
In a similar matter, they gave the solution to instead of using the API, simply handle the route request as follows:
const ipCliente = req.connection.remoteAddress
But the returned ip is ::1
.
I read about this
Ipify
and what it does is return its own public ip, is that right? When I need to capture the ip of a request I wear something like @Supercharge/request-ip next to Express. Did you test with this package? I imagine thatreq.connection.remoteAddress
be from Express, but Voce triedreq.ip
?– Cmte Cardeal
Read the header
X-Forwarded-For
request solves your problem?const ipCliente = req.headers['X-Forwarded-For']
– Augusto Vasques
Gentlemen, both of the suggested commands have returned me ::1 . I’m suspecting it’s some bug related to the host location
– Caio Montenegro
Problem solved! As I was working with tests on localhost, the req.ip method was returning ::1 because it is a prefix of the future ipv6. When testing already in EC2 the IP has been rescued correctly. Thank you for all your help!
– Caio Montenegro