16

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

Ben Dowling
  • 17,187
  • 8
  • 87
  • 103
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

2 Answers2

12

Have you looked at the node.js modules page?

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

Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
  • that page has been deprecated and I found this one that looks better than all the above: https://github.com/bluesmoon/node-geoip – coiso Jun 15 '14 at 15:58
1

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.

Ben Dowling
  • 17,187
  • 8
  • 87
  • 103
  • I've tried that but switched over to [`node-maxmind-db`](https://github.com/PaddeK/node-maxmind-db) because it has both sync *and* async API, same method calls support IPv4 *and* IPv6, and it works with the newer MaxMind format. – Camilo Martin May 31 '15 at 05:11