Open In App

Node.js dns.resolve() Method

The dns.resolve() method is an inbuilt application programming interface of the dns module which is used to resolve hostname into an array of the resource records.

Syntax:



dns.resolve( hostname, rrtype, callback )

Parameters: This method accept three parameters as mentioned above and described below:

records: ["34.218.62.116"]

Example 2:




// Node.js program to demonstrate the   
// dns.resolve() method
  
// Accessing dns module
const dns = require('dns');
  
// Set the rrtype for dns.resolve() method
const rrtype="MX";
  
// Calling dns.resolve() method for hostname
// geeksforgeeks.org and print them in
// console as a callback
dns.resolve('geeksforgeeks.org', rrtype, (err, records)
             => console.log('records: %j', records));

Output:

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

Example 3:




// Node.js program to demonstrate the   
// dns.resolve() method
  
// Accessing dns module
const dns = require('dns');
  
// Set the rrtype for dns.resolve() method
const rrtype="TXT";
  
// Calling dns.resolve() method for hostname
// geeksforgeeks.org and print them in
// console as a callback
dns.resolve('geeksforgeeks.org', rrtype, (err, 
    records) => console.log('records: %j', records));

Output:

records: [
    ["v=spf1 include:amazonses.com include:_spf.google.com -all"],
    ["fob1m1abcdp777bf2ncvnjm08n"]
]

Example 4:




// Node.js program to demonstrate the   
// dns.resolve() method
  
// Accessing dns module
const dns = require('dns');
  
// Set the rrtype for dns.resolve() method
const rrtype="NS";
  
// Calling dns.resolve() method for hostname
// geeksforgeeks.org and print them in
// console as a callback
dns.resolve('geeksforgeeks.org', rrtype, (err, 
    records) => console.log('records: %j', records));

Output:

records: [
    "ns-1520.awsdns-62.org", 
    "ns-1569.awsdns-04.co.uk",
    "ns-245.awsdns-30.com",
    "ns-869.awsdns-44.net"
]

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

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


Article Tags :