Node.js - dns.resolveMx() Method

Node.jsServer Side ProgrammingProgramming

The dns.resolveMx() method uses the DNS protocol to resolve mail exchange (MX) records for the hostname. The addresses argument passed to the callback function will contain an array of objects containing both the priority and exchange objects.

Syntax

dns.resolveMx(hostname, callback)

Parameters

  • hostname – This parameter takes input for hostname to be resolved.

  • callback – This function will catch errors, if any.

    • records – Returns Mx records for the hostname.

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');

// Passing the argument below
dns.resolveMx('tutorialspoint.com', (err, records) => console.log('addresses: %j', records));

Output

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

Example 2

// dns.resolveMx() Demo Example

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

// Passing the argument below
dns.resolveMx('google.com', (err, records) => console.log('addresses: %j', records));

Output

C:\home\node>> node resolveMx.js
addresses: [{"exchange":"alt1.aspmx.l.google.com","priority":20},{"exchange":"alt4.aspmx.l.google.com","priority":50},{"exchange":"alt2.aspmx.l.google.com","priority":30},{"exchange":"alt3.aspmx.l.google.com","priority":40},{"exchange":"aspmx.l.google.com","priority":10}]
raja
Published on 17-Jan-2022 12:56:09

Advertisements