We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with fact开发者_StackOverflows and citations.
Closed 7 years ago.
Improve this questionGive an IP, is there a node.js module that can determine what city and state it is in?
Have you looked at the node.js modules page?
It lists GeoIP and node-geoip and node-maxmind and node-maxmind-native.
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.
精彩评论