如何在node.js和mongodb中实现geoip

我在node.js中看到了一些使用geoip的例子,例如https://github.com/kuno/GeoIP.git和https://github.com/wadey/node-geoip 。 然而,我想要的是显示地图显示geoip为特定的用户。如何实现。

你可以得到一个地理位置数据库(如http://www.maxmind.com ),并将其存储在mongo中。 每个logging包含一个IP范围(开始/结束)和与该IP范围相关的经度/纬度。 IP被表示为整数。 你可以在start字段创build一个索引,然后在mongo上查询,查找起始值最大值小于客户端IP的logging,查找相应的经纬度。

至于绘制具有此经纬度的地图,可以非常容易地创build以特定位置为中心的Google地图:(查看源代码: http : //code.google.com/apis/maps/documentation/javascript/ v2 / examples / map-simple.html )

有很多不同的方式来存储/查询地理位置数据,但这只是一个可能的方法,使用mongo可能工作。 希望这可以帮助。

GeoIP的最好的模块是https://github.com/kuno/GeoIP在我看来(我实际上使用这个模块的一个项目,它完美的工作)。 您必须从Maxmind下载数据库,并安装一些特定于操作系统的库,然后编译该模块。

除非您想要在多个服务器之间轻松复制,否则不需要将数据库放到MongoDB中。 您可以将数据库放入文件中,并在Node.js模块中提供path。

这里是一个例子:

 // Open the GeoLiteCity.dat file first. var City = geoip.City; var city = new City('/path/to/GeoLiteCity.dat'); console.log(city); // this contains country, city, lat, long, continent, postal code etc 

Python实现:

 #!/usr/bin/python #coding: utf-8 import os import pygeoip gi = pygeoip.GeoIP('GeoIP.dat') gic = pygeoip.GeoIP('GeoIPCity.dat') fl = file(r'apache-unique.log') lines = fl.readlines() for line in lines: print gi.country_code_by_addr(line) print gic.record_by_addr(line) os.system('pause') 

我刚刚为我创build的IPLocate.io API发布了一个NPM模块,它可以让您根据IP地址find位置(城市,国家和坐标)。

超级简单,无需下载数据库,每天有1500个免费请求。

安装

npm install node-iplocate

用法

 const iplocate = require("node-iplocate"); iplocate("8.8.8.8").then(function(results) { console.log("IP Address: " + results.ip); // IP Address: 8.8.8.8 console.log("Country: " + results.country + " (" + results.country_code + ")"); // Country: United States (US) console.log("Continent: " + results.continent); // Continent: North America console.log("Organisation: " + results.org + " (" + results.asn + ")"); // Organisation: Google LLC (AS15169) console.log(JSON.stringify(results, null, 2)); /* { "ip": "8.8.8.8", "country": "United States", "country_code": "US", "city": null, "continent": "North America", "latitude": 37.751, "longitude": -97.822, "time_zone": null, "postal_code": null, "org": "Google LLC", "asn": "AS15169" } */ }); // Or with callbacks iplocate("8.8.8.8", null, function(err, results) { // ... console.log(JSON.stringify(results, null, 2)); }); // Provide an API key from IPLocate.io iplocate("8.8.8.8", { api_key: "abcdef" }).then(function(results) { // ... }); 

试试IP2Location Node.js

https://github.com/ip2location-nodejs/IP2Location

先决条件

http://lite.ip2location.com/下载免费的LITE数据库,并在下面使用。

安装

 npm install ip2location-nodejs 

用法

 var ip2loc = require("ip2location-nodejs"); ip2loc.IP2Location_init("/path_to_your_database_file/your_BIN_file.BIN"); testip = ['8.8.8.8', '2404:6800:4001:c01::67', '2001:0200:0102:0000:0000:0000:0000:0000', '2001:0200:0135:0000:0000:0000:0000:0000', '2001:0200:017A:0000:0000:0000:0000:0000']; for (var x = 0; x < testip.length; x++) { result = ip2loc.IP2Location_get_all(testip[x]); for (var key in result) { console.log(key + ": " + result[key]); } } 

扩展mpobrien的答案 –

我发现这是更容易和更快的方法。

在maxmind下载二进制数据库

http://dev.maxmind.com/geoip/geoip2/geolite2/ http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz

使用node-maxmind-db

https://github.com/PaddeK/node-maxmind-db

 var mmdbreader = require('maxmind-db-reader'); // open database var countries = mmdbreader.openSync('./countries.mmdb'); // get geodata app.get('/api/v1/ip/', function(req, res) { var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; countries.getGeoData(ip, function(err, geodata) { if(!err && geodata.location) return res.json({success: true, location: geodata.location}); return res.json({success: false, location: null, error: err}); }); }); 

然后你将有基于他的IP地址,你可以在地图上显示的用户位置。

Benefits-

绑定数据文件很小,易于更新。 没有数据库导入导出。 只需交换文件并重新加载数据。 ie- mmdbreader.openSync()