IP lookup using Maxmind databases
npm i maxmind
- Country/Region/Location lookup by IP (v4 and v6)
- Distance between two IP addresses (locations)
- Timezone lookup by IP
- Autonomous System Numbers (ASN) lookup by IP
- Network speed lookup by IP
** see code samples in
./examples directory **
var maxmind = require'maxmind';// City/Location lookupmaxmindinit'/path/to/GeoLiteCity.dat';var location = maxmindgetLocation'18.104.22.168';// Country Lookupmaxmindinit'/path/to/GeoIP.dat';var country = maxmindgetCountry'22.214.171.124';// Autonomous System Numbers (ASN) lookupmaxmindinit'/path/to/GeoIPASNum.dat';var org = maxmindgetAsn'126.96.36.199';// Internet Service Provider (ISP) lookupmaxmindinit'/path/to/GeoISP.dat';var org = maxmindgetIsp'188.8.131.52';// Net Speed lookupmaxmindinit'/path/to/GeoIPNetSpeedCell.dat';var speed = maxmindgetNetSpeed'184.108.40.206';// Organization lookupmaxmindinit'/path/to/GeoIPOrg.dat';var org = maxmindgetOrganization'220.127.116.11';
Module is fully campatible with IPv6 maxmind databases. Make sure you initialize with proper IPv6 databases before making queries.
maxmindinit'/path/to/GeoLiteCityV6.dat';var location = maxmindgetLocationV6'2001:4860:0:1001::3004:ef68';
All methods works in the same way as for IPv4, the only difference is
V6 postfix in method names:
You can initialize module with several databases at once, and proper db will be automatically selected for particular query. If any option is given it applies to all databases you initialize.
var maxmind = require'maxmind';maxmindinit'/path/to/GeoLiteCity.dat' '/path/to/GeoIPASNum.dat';// now both org and location lookups will workvar org = maxmindgetOrganization'18.104.22.168';var location = maxmindgetLocation'22.214.171.124';
By default module does not use cache, and works directly with file system. Enabling cache leads to better performance though consumes more memory.
indexCachesaves in memory the country index only
memoryCachesaves in memory full database file
checkForUpdateschecks databases for updates (via fs mtime). Basically once you replace the old DB file with the new one module automamtically re-initialises.
Options can be passed to
var maxmind = require'maxmind';maxmindinit'/path/to/GeoIP.dat' indexCache: true checkForUpdates: true;
Caching significantly increases performance, refer to this camparison which was made on average laptop:
- default: 20,000 lookups / second
indexCache: 115,000 lookups / second
memoryCache: 270,000 lookups / second
Following benchmark is made for
GeoIPCity database. Memory caching is enabled where possible. If you believe that
benchmark is not realistic please post a PR and share your code :)
node-maxmind 274649 op/secgeoip-lite 191681 op/sec 43.28% slowergeoip 43483 op/sec 531.61% slower
Make sure you run
npm i command in the project's dir before you begin, it'll install all dev dependencies. Currently
code coverage is about 85%, so new tests are essential when you add new functionality. There're several npm tasks
which you can find useful:
npm testruns tests
npm run lintruns js linter
npm run coverageruns code coverage task and generates report
npm run benchmarkruns basic benchmark
One pull request per one feature, nothing unusual.
- Timezones http://www.maxmind.com/timezone.txt
- Region codes http://www.maxmind.com/app/iso3166_2