Node.js – dnsPromises.reverse() 方法


dnsPromises.reverse() 方法執行反向 DNS 搜尋以將 IPv4 或 Ipv6 地址解析為主機名陣列。如果沒有遇到成功狀態,該 Promise 將會被錯誤物件拒絕。

語法

dnsPromises.reverse( ip )

其中,ip 是一個引數,用於獲取要解析的 IP 地址的輸入。

示例 1

建立一個名為 "reverse.js" 的檔案,並複製以下程式碼段。建立檔案後,使用 "node reverse.js" 命令執行此程式碼,如下例所示 −

// dns.reverse() Demo Example

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

const dnsPromises = dns.promises;

// Passing IP to find the DNS name
dnsPromises.reverse('95.217.74.146').then((response) => {
   console.log("Resolved DNS is:", response);
})

輸出

C:\home
ode>> node reverse.js Resolved DNS is: ['static.146.74.217.95.clients.your-server.de']

示例 2

// dns.reverse() Demo Example

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

const dnsPromises = dns.promises;

// Setting ttl as true
const options={
   ttl:true,
};

// Calling dnsPromises.reverse() method
// asynchronously
(async function() {

   const records = await dnsPromises.reverse('127.0.0.8', options);

   // Printing records
   console.log("DNS: ", records);
})();

輸出

C:\home
ode>> node reverse.js DNS: [ 'localhost' ]

更新於: 17-Jan-2022

129 瀏覽量

開啟你的職業生涯

透過完成本課程獲得認證

開始
廣告
© . All rights reserved.