Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 72 lines (47 sloc) 2.75 kb

GeoIP API for node.

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' });

  • Set all properties of an existing data object to undefined. 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 
                                                    //}   
    
Something went wrong with that request. Please try again.