Skip to content

onelesd/koa-geo-ip

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-geo-ip

Uses ip to geolocate requests

Getting Started

npm i koa-geo-ip

Example

Example using as koa middleware

var app = require( 'koa' )();
var geo = require( 'koa-geo-ip' );

app.use( geo() );

app.use( function* () {
  this.body = this.geo;
});

app.listen( 3000 );

Example using co to access the generator

var co = require( 'co' );
var geoip = require( 'koa-geo-ip' );
var geolocate = geoip();

co( function *() {
  var location = {
    ip: '8.8.8.8'
  };
  yield gg.call( location );
  return location.geo;
})
  .then( function( loc ) {
    console.log( loc );
  });

Result (excerpt)

{
  country: {
    geoname_id: 6252001,
    iso_code: 'US'
  }
}

Running the examples

You’ll be needing the db, there’s a script included for that

npm run geoip

node examples/co.js
node examples/koa.js

API

Options

db <String> path to the MaxMind GeoLite2 database

The db path can also be specified using the package.json, see this repo’s package for an example. Both the install script and the module will use the package info if run via npm, otherwise specify the path.

Install Script

Included is a quick script to download the database, by default it’ll use the path specified in the package.json if run via npm, use --db to specify a path.

node bin/geoip --db path/to/db specify a path node bin/geoip --update will force a db update

License

WTFPL

Copyright (c) 2015, Matt Styles

This product includes GeoLite2 data created by MaxMind, available from http://www.maxmind.com

About

Uses ip to append geolocation

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%