Node.js - dnsPromises.resolveMx() Method



The dnsPromises.resolveMx() method uses the DNS protocol to resolve the mail exchange records (MX Records) for the hostname. The promise is resolved with an array of objects containing both a priority and exchange property on success.

Syntax

dnsPromises.resolveMx( hostname )

where, hostname is the parameter that takes the input for the hostname to be resolved.

Example 1

Create a file with the name "resolveMx.js" and copy the following code snippet. After creating the file, use the command "node resolveMx.js" to run this code as shown in the example below −

// 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); })

Output

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 } ]

Example 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); })();

Output

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 } ]
Updated on: 2022-01-17T12:43:28+05:30

286 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements