koa-geoip

1.0.4 • Public • Published

koa-geoip

IP-based geolocation middleware for Koa

Installation (via npm)

$ npm install koa-geoip

Usage

var koa = require('koa');
var route = require('koa-route');
var geolocate = require('koa-geoip');
var app = koa();

app.use(geolocate());

app.use(route.get('/', function() {
  if (this.request.geolocation) {
    this.body = {
      city: this.request.geolocation.city,
      state: this.request.geolocation.region,
      country: this.request.geolocation.country,
      latlng: this.request.geolocation.ll
    }
  } else {
    this.body = {error: "Could not geolocate."};
  }
}));

app.listen(3000);

Options

whitelist

If the whitelist option is not provided, koa-geoip will be used on all routes. The whitelist option allows you to specify which routes the middleware should be used on.

app.use(geolocate({
  whitelist: ['/foo', '/bar', '/baz']
}));

Dependencies

node-geoip

License

MIT License

Readme

Keywords

none

Package Sidebar

Install

npm i koa-geoip

Weekly Downloads

24

Version

1.0.4

License

MIT

Last publish

Collaborators

  • lrdiv