【问题标题】:How to use Geoip to get Current city name on NodeJs如何使用 Geoip 在 NodeJs 上获取当前城市名称
【发布时间】:2017-12-04 11:22:44
【问题描述】:

我想在Nodejs上应用Geoip来获取我现在所在城市的名字,然后我尝试通过Npm安装Geoip包,但是在Windows上运行时似乎出现了一些错误,所以我应该怎么做才能解决这个问题.

【问题讨论】:

  • 请显示您遇到的错误以及您尝试过的方法,以便人们知道从哪里开始。
  • 没有错误,Geoip 包只是不支持 Windows。所以我想问的是是否有一些解决方案。

标签: javascript node.js geoip


【解决方案1】:

To get Country or City from IP 试试这个。非常有用。

var geoip = require('geoip-lite');

https://github.com/bluesmoon/node-geoip

$ npm install geoip-lite

var ip = "207.97.227.239";
var geo = geoip.lookup(ip);

console.log(geo);

{ 
  range: [ 3479297920, 3479301339 ],
  country: 'US',
  region: 'TX',
  city: 'San Antonio',
  ll: [ 29.4889, -98.3987 ],
  metro: 641,
  zip: 78218 
}

【讨论】:

    【解决方案2】:

    我刚刚为我创建的IPLocate.io API 发布了一个 NPM 模块。

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

    安装

    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) {
      // ...
    });
    

    【讨论】:

      【解决方案3】:

      试试 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]);
          }
      }
      

      【讨论】:

        【解决方案4】:

        如果有人正在寻找免费的内存解决方案: https://www.npmjs.com/package/@avindak/xgeoip

        const geoip = require('@avindak/xgeoip');
        
        await geoip.load_memory();
        //AU
        let res = await geoip.resolve("1.1.1.1");
        console.log(res.country_code);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-29
          • 1970-01-01
          • 2013-08-15
          • 1970-01-01
          • 2010-11-25
          • 2010-11-25
          相关资源
          最近更新 更多