Node.js | dnsPromises.resolveMx() Method

The dnsPromises.resolveMx() method is an inbuilt application programming interface of promises object of the dns module which is used to resolve MX or mail exchange records for the specified hostname using DNS protocol.

Syntax:

dnsPromises.resolveMx( hostname )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • hostname: this parameter specifies a string which denotes the hostname to be resolved.

Return Value: This method returns error, addresses.

Below examples illustrate the use of dnsPromises.resolveMx() method in Node.js:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the   
// dnsPromises.resolveMx() method
  
// Accessing promises object from dns module
const dns = require('dns');
const dnsPromises = dns.promises;
   
// Calling dnsPromises.resolveMx() method 
dnsPromises.resolveMx('google.com').then((res) => {
    console.log("for google : ");
    console.log(res);
});

chevron_right


Output:

for google :
[ { exchange: 'alt4.aspmx.l.google.com', priority: 50 },
  { exchange: 'alt3.aspmx.l.google.com', priority: 40 },
  { exchange: 'aspmx.l.google.com', priority: 10 },
  { exchange: 'alt1.aspmx.l.google.com', priority: 20 },
  { exchange: 'alt2.aspmx.l.google.com', priority: 30 } ]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the   
// dnsPromises.resolveMx() method
  
// Accessing promises object from dns module
const dns = require('dns');
const dnsPromises = dns.promises;
   
// Calling dnsPromises.resolveMx() method 
// asynchronously 
(async function() {
      
    // Records from resolveMx function
    const records = await dnsPromises.resolveMx(
                        'geeksforgeeks.org');
      
    // Printing  records
    console.log("from async: ");
    console.log(records);   
})();

chevron_right


Output:

from async:
[ { exchange: 'aspmx.l.google.com', priority: 1 },
  { exchange: 'alt3.aspmx.l.google.com', priority: 10 },
  { exchange: 'alt4.aspmx.l.google.com', priority: 10 },
  { exchange: 'alt1.aspmx.l.google.com', priority: 5 },
  { exchange: 'alt2.aspmx.l.google.com', priority: 5 } ]

Note: The above program will compile and run by using the node index.js command.

Reference: https://nodejs.org/api/dns.html#dns_dnspromises_resolvemx_hostname




My Personal Notes arrow_drop_up

Recommended Posts:

    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.