ip地址查询(GeoIP)

因为在系统中要用到通过IP地址来判断该IP地址的地区,然后通过地区编码进行targeting,刚开始想到的是网上有很多在线的服务,可以实现这个功能。。。但是觉得这样子每次查询速度都太慢,不能满足系统的要求,于是试着在github上面去找类似的东西,没想到找到了这个神器。。。

https://github.com/kuno/GeoIP


可以是把它看做是离线的ip地址转换包,只需要下一个离线的数据库GeoIP.dat就可以用了。。。废话不说。。直接上demo代码:

var geoip = require('geoip');

// !! Important !!
// Befor you can use this package, you need to download or buy some data fro http://www.maxmind.com/app/ip-location.
// There are some free databases among some commercial versions, the free databases can be found http://geolite.maxmind.com/download/geoip/database/.



// Country

/
// ipv4 address lookup //
/
var Country = geoip.Country;
var country = new Country('./GeoIP.dat');

// Synchronous method(the recommended way):
var country_obj = country.lookupSync('115.253.94.119');

console.log(country_obj);

输出的内容如下:

{ country_name: 'India',
  country_code: 'IN',
  country_code3: 'IND',
  continent_code: 'AS' }


真心很不错,很好用。。留存。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值