Node.js - dnsPromises.resolve4() 方法


dnsPromises.resolve4() 方法使用 DNS 協議解析主機名的 IPv4 地址(A 記錄)。如果為 True,則會使用 IP 地址陣列解決一個承諾。

dnsPromisesdns 模組之間的區別在於,dnsPromises 提供了非同步 DNS 方法的替代方式,這些方法返回 Promise 物件,而不是回撥函式。

語法

dns.resolve4(hostname, [options])

引數

  • hostname - 此引數將要解析的主機名作為輸入。

  • options - 可以具有以下選項 -

    • ttl - 定義每條記錄的生存時間 (TTL)。回撥函式會像這樣接收一個地址陣列 - { address: '1.2.3.4', ttl:60 }

示例 1

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

// dns.resolve4() Demo Example

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

// Passing a single dns to get values
dnsPromises.resolve4('tutorialspoint.com').then((response) => {
   console.log("Resolved address is:", response);
})

輸出

Resolved address is: [ '95.217.74.146' ]

示例 2

// dns.resolve4() Demo Example

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

const dnsPromises = dns.promises;

const options = {
   ttl:true,
};

// Calling resolve4 using promises
(async function() {

   // Passed dns value to be resolved
   const records = await dnsPromises.resolve4( 'tutorialspoint.com', options);

   // Printing records
   console.log(records);
})();

輸出

[ { address: '95.217.74.146', ttl: 267 } ]

更新於: 2021-10-29

282 次瀏覽

開啟你的 職業生涯

透過完成教程獲得認證

開始學習
廣告