因为在系统中要用到通过IP地址来判断该IP地址的地区,然后通过地区编码进行targeting,刚开始想到的是网上有很多在线的服务,可以实现这个功能。。。但是觉得这样子每次查询速度都太慢,不能满足系统的要求,于是试着在github上面去找类似的东西,没想到找到了这个神器。。。
可以是把它看做是离线的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' }
真心很不错,很好用。。留存。。。