Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
77 lines (50 sloc) 2.85 KB

GeoIP API for node.

Warning: Due to the Asynchronous programming problome, Any version that below the 0.1.3 dosen't work in any real script. I highly recommond that every user should update to at least v0.1.3!

Description

Get geolocation information based on domain or IP address.

Data

Befor you can use this package, you need to download or buy some data from www.maxmind.com.

There are two free versions data among with some commercial versions.

Note: This package only support binary data, not any other formats.

GeoIP Country Lite Edition Download.

GeoIP City Lite Edition Download

Install

npm install geoip

Usage

Country Information

  • Open the country data file

    var data = geoip.open('/path/to/GeoIP.dat');

  • Synchronous methods, network independence.

    geoip.Country.code_by_addr(data, '8.8.8.8'); // prints 'US'

    geoip.Country.name_by_addr(data, '8.8.8.8'); // prints 'United States'

  • Asynchronous methods, depends on node's async-style dns module.

    geoip.Country.code_by_domain(data, 'www.google.com', function(err, code) { if (err) {throw err;} console.log(code); // prints 'US' });

    geoip.Country.name_by_domain(data, 'www.google.com', function(err, name) { if (err) {throw err;} console.log(name); // prints 'United States' });

  • Close the opened file. geoip.close(data);

City Information

  • Open the GeoLiteCity.dat file first.

    var data = geoip.open('/path/to/GeoLiteCity.dat');

    geoip.City.record_by_addr(data, '8.8.8.8'); // You will get something like this:

                                                    //{ 
                                                    //country_code: 'US',
                                                    //country_code3: 'USA',
                                                    //country_name: 'United States',
                                                    //continet_code: 'NA',
                                                    //region: 'CA',
                                                    //city: 'Mountain View',
                                                    //postal_code: '94043',
                                                    //latitude: 37.41919999999999,
                                                    //longitude: -122.0574,
                                                    //dma_code: 807,
                                                    //metro_code: 807,
                                                    //area_code: 650 
                                                    //}   
    
Jump to Line
Something went wrong with that request. Please try again.