![](/img/trans.png)
[英]Is it possible to find device geo location based on ip address using NodeJS?
[英]Finding Location through IP address Nodejs mongodb
我正在嘗試查找 IP 地址的位置。 The IP address will be sent to the API through a front end android/iOS application(I'm currently using a static IP address of my computer). 我想使用客戶端 IP 地址來確定它們可能的物理位置,並將該位置發送到 MongoDB 進行存儲。 對此的任何幫助將不勝感激。 提前致謝。
您可以使用GeoIP-lite從IP地址獲取地理數據。
var geoip = require('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 }*/
你可以使用freegeoip.net :
freegeoip.net/{format}/{IP_or_hostname}
樣本GET http://freegeoip.net/json/207.97.227.239 :
{
ip: "207.97.227.239",
country_code: "US",
country_name: "United States",
region_code: "TX",
region_name: "Texas",
city: "San Antonio",
zip_code: "78218",
time_zone: "America/Chicago",
latitude: 29.4889,
longitude: -98.3987,
metro_code: 641
}
限制:
默認情況下,每小時最多允許10,000個查詢。 達到此限制后,所有請求都將導致HTTP 403被禁止,直到您的配額被清除。
freegeoip Web服務器是免費和開源的,因此如果公共服務限制對您來說是一個問題,請下載並運行您自己的實例。
如果您只需要國家/地區精度,則可以使用cloudflare ip geoloc ,它會在請求中添加標頭,因此無需額外的API調用。
您可以將Apiip.net package用於 Node.js:
用法很簡單:
const apiip = require('apiip.net')('your_api_key');
apiip
.getLocation({
ip: '67.250.186.196'
})
.then((results) => console.log(results))
.catch((error) => console.error(error));
或者,如果您願意,只需使用簡單的 GET 調用:
https://apiip.net/api/check?ip=67.250.186.196&accessKey={your_api_key}
他們提供了很多關於 IP 的信息,不僅僅是位置:
{
"ip": "67.250.186.196",
"continentCode": "NA",
"continentName": "North America",
"countryCode": "US",
"countryName": "United States",
"countryNameNative": "United States",
"city": "New York",
"postalCode": "10001",
"latitude": 40.8271,
"longitude": -73.9359,
"capital": "Washington D.C.",
"phoneCode": "1",
"countryFlagEmoj": "🇺🇸",
"countryFlagEmojUnicode": "U+1F1FA U+1F1F8",
"isEu": false,
"languages": {
"en": {
"code": "en",
"name": "English",
"native": "English"
}
},
"currency": {
"code": "USD",
"name": "US Dollar",
"symbol": "$",
"number": "840",
"rates": {
"EURUSD": 1.11
}
},
"timeZone": {
"id": "America/New_York",
"currentTime": "10/26/2021, 2:54:10 PM",
"code": "EDT",
"timeZoneName": "EDT",
"utcOffset": -14400
},
"connection": {
"asn": 12271,
"isp": "Charter Communications Inc"
},
"security": {
"isPublicProxy": false,
"isResidentialProxy": false,
"isTorExitNode": false,
"network": "67.250.176.0/20"
}
}
您可以使用IPinfo 的 API 、 IPinfo 的 Nodejs 模塊,或者如果您使用的是 Express.js,您可以使用IPinfo 的 Express 客戶端庫。
node-ipinfo
適用於 ES5、ES6+ 和 Typscript。 安裝客戶端庫:
npm install node-ipinfo
然后你可以按如下方式使用它 -
const { IPinfoWrapper } = require("node-ipinfo");
const ipinfo = new IPinfoWrapper("MY_TOKEN");
ipinfo.lookupIp("1.1.1.1").then((response) => {
// replace 1.1.1.1 with an IP Address
console.log(response);
});
該庫非常健壯且功能豐富,因此我建議您查看文檔。
如果要使用 API,可以通過修改以下格式進行GET
調用:
ipinfo.io/IP_ADDRESS?token=YOUR_TOKEN
在每月 50,000 個請求的免費套餐中,您將收到如下響應:
{
ip: '1.1.1.1',
hostname: 'one.one.one.one',
anycast: true,
city: 'Los Angeles',
region: 'California',
country: 'United States',
loc: '34.0522,-118.2437',
org: 'AS13335 Cloudflare, Inc.',
postal: '90076',
timezone: 'America/Los_Angeles',
countryCode: 'US'
}
你可以試試免費的IP2Location.io API。 只需在https://www.ip2location.io/注冊免費的 API 密鑰,然后使用下面的 Node.js 代碼來調用 Node.js 代碼即可調用 https:
var https = require("https");
const BASE_URL = "https://api.ip2location.io/";
let data = {
key: "YOUR_API_KEY", // IP2Location.io API key
ip: "8.8.8.8" // IP address to query
};
let url = BASE_URL + "?";
Object.keys(data).forEach(function (key, index) {
if (this[key] != "") {
url += key + "=" + encodeURIComponent(this[key]) + "&";
}
}, data);
url = url.substring(0, url.length - 1);
let d = "";
let req = https.get(url, function (res) {
res.on("data", (chunk) => (d = d + chunk));
res.on("end", function () {
console.log(JSON.parse(d));
});
});
您應該看到如下結果:
{
ip: '8.8.8.8',
country_code: 'US',
country_name: 'United States of America',
region_name: 'California',
city_name: 'Mountain View',
latitude: 37.405992,
longitude: -122.078515,
zip_code: '94043',
time_zone: '-07:00',
asn: '15169',
as: 'Google LLC',
is_proxy: false
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.