Open In App

Node.js dnsPromises.resolveMx() Method

Last Updated : 13 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


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:




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


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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads