Node.js – dns.getServers() 方法


dns.getServers() 方法返回一個 IP 地址字串陣列。地址將按照 RFC 5952 標準進行格式化,該標準針對 DNS 解析而配置。如果使用了自定義埠,該字串還將包含埠部分。

語法

dns.getServers()

引數

由於它返回伺服器列表,因此無需任何引數。

示例 1

建立一個檔案 "getServers.js" 並複製以下程式碼段。建立檔案後,使用命令 "node getServers.js" 執行此程式碼。

// dns.getServers() Node js Example

// Importing the dns module
const dns = require('dns');

// Reading the IP related info
// for the current host
console.log(dns.getServers());

輸出

[ '213.133.98.98', '213.133.100.100', '213.133.99.99' ]

示例 2

// dns.getServers() Node js Example

// Importing the dns module
const dns = require('dns');

// Reading the IP related info
// for all hosts
allServers = dns.getServers();

// Iterating over all the IP's and printing them
allServers.forEach(element => {
   console.log(element);
});

輸出

213.133.98.98
213.133.100.100
213.133.99.99

更新於: 2021 年 10 月 29 日

115 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

立即開始
廣告