2

I am using GeoIP to get country from client address but it returns null

 ip = (req.headers['x-forwarded-for'] || '').split(',')[0] || req.connection.remoteAddress;
 country = geoip.lookup(ip).country;

I think this is because I am using localhost since the detected ip is 127.0.1. How to solve that?

Vinz243
  • 9,654
  • 10
  • 42
  • 86

1 Answers1

2

You can use services like http://fugal.net/ip.cgi or http://ifconfig.me/ip to get your external IP address via http.get() or run a shell command using child_process.exec() (like $ ip on OS X), but it's not a cross-platform solution. I don't think it's possible to get local machine external IP using http.IncomingMessage object (req) or os.networkInterfaces() method.

In your case, I would default to some country/latitude/whatever you need in case of null from geoip.lookup(), at least in development environment.

var geo = geoip.lookup(ip);
var country = geo ? geo.country : 'US';
  • Thanks, that is what I will do but I will have to wait production to test whether it works or not :( – Vinz243 May 21 '14 at 17:24