Node.js - dns.resolveMx() Method


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

Updated on: 17-Jan-2022

263 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements