1.0.3 • Published 7 years ago

koa-location v1.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

koa-location

Koa middleware getting location information about client via user's IP.

Install


Install with npm

$ npm install koa-location --save

Usage

This module works detects the location of client by requests to following providers:

Example of the object you will get with location information

{
 code: "BG",
 country: "Bulgaria",
 region: "Burgas",
 city: "Burgas",
 latitude: 42.5,
 longitude: 27.4667
}
Example without auto detecting of location
var Koa = require('koa');
var app = new Koa();
var location = require('koa-location');

app.use(location());

app.use(async (ctx, next) => {
	await ctx.request.detect();
	ctx.body = 'Ce Kavo';
	console.log(ctx.request.location); // => getting the location info
});

var port = 8000;
app.listen(port, ()=> {
	console.log('Сервер работает на порту ' + port);
});
Example with auto detecting of location
var Koa = require('koa');
var app = new Koa();
var location = require('koa-location');

app.use(location({
	autoDetect: true
}));

app.use(async (ctx, next) => {
	ctx.body = 'Ce Kavo';
	console.log(ctx.request.location);  // => getting the location info
});

var port = 8000;
app.listen(port, ()=> {
	console.log('Сервер работает на порту ' + port);
});

If you not set to true the autoDetect option you need for calling additionaly the detect() function of the request object.

By default, autoDetect is false.

Support for FreeUlrs when autoDetect is true

When autoDetect is true, the detecting of location will not runs.

var Koa = require('koa');
var app = new Koa();
var location = require('koa-location');

app.use(location({
	autoDetect: true,
	freeUrls: ['/', '/cekavo','/hello']
}));

app.use(async(ctx, next) => {
	ctx.body = 'Ce Kavo';
	// detecing will runs if request url is not included in the array of free urls
	console.log(ctx.request.location); 
});

var port = 8000;
app.listen(port, () => {
	console.log('Сервер работает на порту ' + port);
});
1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.0

7 years ago