Give an IP, is there a node.js module that can determine what city and state it is in?
publicIp. v4(). then(ip => { console. log("your public ip address", ip); });
An IP Address node represents an IPv4 or IPv6 address of a host or a device.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With