在 Node 中確定使用者 IP 地址
Node.js 是一個完全開源技術,執行在 JavaScript 執行時環境中。當用戶想要訪問網站或連結時,他們透過系統 IP 連線連結。我們可以在 Node 中使用 dns.lookup() 方法查詢當前使用者的 IP 地址。
語法
dns.lookup(hostname, [options], callback)
引數
引數如下所述 −
hostname − 這個輸入引數包括有效或活動的網路連結。
options − 預設為 0。它接收 IP 型別的輸入,即 Ipv4 的 4 和 Ipv6 的 6。
callback − 處理發生的任何錯誤
示例 1
建立一個名為 "ipAddress.js" 的檔案,並複製程式碼段。建立檔案後,使用命令 "node ipAddress.js" 執行此程式碼。
// Getting IP of user
// Importing the DNS module
const dns = require('dns');
// Pass the user DNS for which IP is needed
dns.lookup('www.tutorialspoint.com',
(err, addresses, family) => {
// Print the IP address of user
console.log('IP Address : ', addresses);
// Print the number of families found
console.log('IP Family: ', family);
});輸出
C:\home
ode>> node ipAddress.js IP Address: 117.18.237.42 IP Family: 4
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP