Node.js - dnsPromises.resolveMx() 方法


dnsPromises.resolveMx() 方法使用 DNS 協議來解析主機名的郵件交換記錄(MX 記錄)。如果成功,promise 將解析為包含優先順序和 exchange 屬性的物件陣列。

語法

dnsPromises.resolveMx( hostname )

其中,hostname 是一個引數,用於輸入要解析的主機名。

示例 1

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

// dns.resolveMx() Demo Example

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

const dnsPromises = dns.promises;

// Passing IP to find the hostname TXT records
dnsPromises.resolveMx('tutorialspoint.com').then((response) => {
   console.log("MX Records: ", response);
})

輸出

C:\home
ode>> node resolveMx.js MX Records: [ { exchange: 'alt3.aspmx.l.google.com', priority: 10 },    { exchange: 'alt2.aspmx.l.google.com', priority: 5 },    { exchange: 'alt1.aspmx.l.google.com', priority: 5 },    { exchange: 'aspmx.l.google.com', priority: 1 },    { exchange: 'alt4.aspmx.l.google.com', priority: 10 } ]

示例 2

// dns.resolveMx() Demo Example

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

const dnsPromises = dns.promises;

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

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

   const records = await dnsPromises.resolveMx('google.com', options);

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

輸出

C:\home
ode>> node resolveMx.js MX Records: [ { exchange: 'alt4.aspmx.l.google.com', priority: 50 },    { exchange: 'aspmx.l.google.com', priority: 10 },    { exchange: 'alt3.aspmx.l.google.com', priority: 40 },    { exchange: 'alt1.aspmx.l.google.com', priority: 20 },    { exchange: 'alt2.aspmx.l.google.com', priority: 30 } ]

更新於: 17-1 月-2022

187 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.