Node.js dnsPromises.resolveMx() Method
Last Updated :
13 Oct, 2021
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:
const dns = require( 'dns' );
const dnsPromises = dns.promises;
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:
const dns = require( 'dns' );
const dnsPromises = dns.promises;
(async function () {
const records = await dnsPromises.resolveMx(
'geeksforgeeks.org' );
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
Share your thoughts in the comments
Please Login to comment...