是否有一个node.js模块可以确定IP的地理位置?

发布于 2024-11-27 14:44:38 字数 1539 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窗影残 2024-12-04 14:44:38

您是否查看过 node.js 模块页面

它列出了 GeoIPnode-geoipnode-maxmindnode-maxmind-native

Have you looked at the node.js modules page?

It lists GeoIP and node-geoip and node-maxmind and node-maxmind-native.

帅气尐潴 2024-12-04 14:44:38

我发现 node-maxmind 是功能最齐全且易于使用的模块。您需要从maxmind 下载页面下载文件,然后就可以使用它像这样:

maxmind = require 'maxmind'
maxmind.init('GeoLiteCity.dat')
maxmind.getLocation('67.188.232.131')

{ countryCode: 'US',
  countryName: 'United States',
  region: 'CA',
  city: 'Mountain View',
  postalCode: '94043',
  latitude: 37.41919999999999,
  longitude: -122.0574,
  dmaCode: 0,
  areaCode: 0,
  metroCode: 0,
  regionName: 'California' }

使用模块的另一种方法是使用地理定位 API,该模块通常需要安装地理定位数据库并定期更新它。我自己就有这样一项服务,http://ipinfo.io。下面是使用优秀的请求模块调用它的示例:

request = require 'request'
request.get('http://ipinfo.io/67.188.232.131', {json: true}, (e, r) -> console.log r.body)

{ ip: '67.188.232.131',
  hostname: 'c-67-188-232-131.hsd1.ca.comcast.net',
  city: 'Mountain View',
  region: 'California',
  country: 'US',
  loc: '37.4192,-122.0574',
  org: 'AS7922 Comcast Cable Communications, Inc.',
  postal: '94043' }

有关更多详细信息,请参阅 http://ipinfo.io/developers

I've found the node-maxmind to be the most feature complete and easy to use module. You need to download the files from the maxmind download page, and then you can use it like this:

maxmind = require 'maxmind'
maxmind.init('GeoLiteCity.dat')
maxmind.getLocation('67.188.232.131')

{ countryCode: 'US',
  countryName: 'United States',
  region: 'CA',
  city: 'Mountain View',
  postalCode: '94043',
  latitude: 37.41919999999999,
  longitude: -122.0574,
  dmaCode: 0,
  areaCode: 0,
  metroCode: 0,
  regionName: 'California' }

An alternative to using a module, which usually requires you to install a geolocation database and regularly update it, is to use a geolocation API. One such service is my own, http://ipinfo.io. Here's an example of calling that using the excellent request module:

request = require 'request'
request.get('http://ipinfo.io/67.188.232.131', {json: true}, (e, r) -> console.log r.body)

{ ip: '67.188.232.131',
  hostname: 'c-67-188-232-131.hsd1.ca.comcast.net',
  city: 'Mountain View',
  region: 'California',
  country: 'US',
  loc: '37.4192,-122.0574',
  org: 'AS7922 Comcast Cable Communications, Inc.',
  postal: '94043' }

See http://ipinfo.io/developers for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文