0

I'm using a node ip taken from here: https://github.com/indutny/node-ip

In my webservice I did a simple thing:

var ip              = require('ip');

module.exports = function(app) {

app.get('/gps', function (req, res) {

    console.log(ip.address());
}
}

I deployed it to my amazon aws account and now whoever enters the page - I constantly see the same ip address in my console log - 172.31.46.96. I tried to check what is this ip (possible option is that it is related to my amazon aws service?), but who.is does not bring the answer.

How should I change my code to see every visitor's ip address instead?

randomuser1
  • 2,733
  • 6
  • 32
  • 68

2 Answers2

1

You're most likely getting an IP of an internal load balancer/proxy and you'll need to configure express to handle that.

This is a good place to start.

Gabriel Isenberg
  • 25,869
  • 4
  • 37
  • 58
  • So I tried to add `app.set('trust proxy', 'loopback, 172.31.46.96')` to my `server.js` file but it didn't help, could you give me some hints what am I doing wrong here? – randomuser1 Feb 09 '16 at 18:54
  • 1
    Does `app.set('trust proxy')`, then using `req.ip` instead of using the node-ip package produce the desired output? If not, do the request headers yield any clues (eg: `req.headers['x-forwarded-for']`)? – Gabriel Isenberg Feb 09 '16 at 18:57
  • Thanks, I think it worked with `trust proxy` and `req.ip`! Just one more question - now when I'm doing `console.log("coordinates"+req.ip)` I'm getting `coordinates::ffff:188.164.244.110` - why is there the `ffff` and how can I get rid of it? – randomuser1 Feb 09 '16 at 19:15
  • According to http://stackoverflow.com/questions/29411551/express-js-req-ip-is-returning-ffff127-0-0-1, you're getting an IPv6 prefix along with your IPv4 address. This library will help if you want to map that to an IPv4 address: https://github.com/whitequark/ipaddr.js – Gabriel Isenberg Feb 09 '16 at 19:38
0

Use req.connection.remoteAddress to get the ip of your user.

Yuri Zarubin
  • 11,439
  • 4
  • 30
  • 33