Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a node.js module that can determine Geolocation of an IP? [closed]

Give an IP, is there a node.js module that can determine what city and state it is in?

like image 844
TIMEX Avatar asked Aug 01 '11 02:08

TIMEX


People also ask

How do I find my public IP address in node JS?

publicIp. v4(). then(ip => { console. log("your public ip address", ip); });

Does node have IP address?

An IP Address node represents an IPv4 or IPv6 address of a host or a device.


2 Answers

Have you looked at the node.js modules page?

It lists GeoIP and node-geoip and node-maxmind and node-maxmind-native.

like image 163
Dan Grossman Avatar answered Oct 13 '22 10:10

Dan Grossman


I've found the node-maxmind to be the most feature complete and easy to use module. You need to download the files from the maxmind download page, and then you can use it like this:

maxmind = require 'maxmind'
maxmind.init('GeoLiteCity.dat')
maxmind.getLocation('67.188.232.131')

{ countryCode: 'US',
  countryName: 'United States',
  region: 'CA',
  city: 'Mountain View',
  postalCode: '94043',
  latitude: 37.41919999999999,
  longitude: -122.0574,
  dmaCode: 0,
  areaCode: 0,
  metroCode: 0,
  regionName: 'California' }

An alternative to using a module, which usually requires you to install a geolocation database and regularly update it, is to use a geolocation API. One such service is my own, http://ipinfo.io. Here's an example of calling that using the excellent request module:

request = require 'request'
request.get('http://ipinfo.io/67.188.232.131', {json: true}, (e, r) -> console.log r.body)

{ ip: '67.188.232.131',
  hostname: 'c-67-188-232-131.hsd1.ca.comcast.net',
  city: 'Mountain View',
  region: 'California',
  country: 'US',
  loc: '37.4192,-122.0574',
  org: 'AS7922 Comcast Cable Communications, Inc.',
  postal: '94043' }

See http://ipinfo.io/developers for more details.

like image 21
Ben Dowling Avatar answered Oct 13 '22 08:10

Ben Dowling