Skip to content
Related Articles

Related Articles

Node.js dns.resolveNs() Method

Improve Article
Save Article
  • Last Updated : 13 Oct, 2021
Improve Article
Save Article

The dns.resolveNs() method is an inbuilt application programming interface of the dns module which is used to resolve NS or name server records for the specified hostname using DNS protocol.

Syntax:

dns.resolveNs( hostname, callback )

Parameters: This method has two parameters as mentioned above and described below:

  • hostname: This parameter specifies a string which denotes the hostname to be resolved.
  • callback: It specifies a function to be called after DNS resolution of the hostnames.
    • error: It specifies error if generated.
    • addresses: It is an array of string that signifies the returned name server records for the hostname.

Return Value: This method returns error, addresses through callback function. These data are passed as parameters to the callback function.

Below examples illustrate the use of dns.resolveNs() method in Node.js:

Example 1:




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


Output:

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

Example 2:




// Node.js program to demonstrate the   
// dns.resolveNs() method
  
// Accessing dns module
const dns = require('dns');
  
// Calling dns.resolveNs() method for
// hostname google.com and displaying
// them in console as a callback
dns.resolveNs('google.com', (err, 
    addresses) => console.log('NS records: %j', addresses));


Output:

NS records: [
    "ns3.google.com",
    "ns2.google.com",
    "ns4.google.com",
    "ns1.google.com"
]

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

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!