Node.js - dnsPromises.resolveNs() Method


The dnsPromises.resolveNs() method uses the DNS protocol to resolve the name server records (NS) for the hostname. On success, the promise is resolved with an array of name server records available for the hostname.

Syntax

dnsPromises.resolveNs( hostname )

where hostname is the parameter that takes the input for the hostname to be resolved.

Example 1

Create a file with the name "resolveNs.js" and copy the following code snippet. After creating the file, use the command "node resolveNs.js" to run this code as shown in the example below −

// dns.resolveNs() Demo Example

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

const dnsPromises = dns.promises;

// Passing a single dns to get values
dnsPromises.resolveNs('tutorialspoint.com').then((response) => {
   console.log("Resolved address is:", response);
})

Output

C:\home
ode>> node resolveNs.js Resolved address is: [ 'pdns14.domaincontrol.com', 'pdns13.domaincontrol.com' ]

Example 2

// dns.resolveNs() Demo Example

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

const dnsPromises = dns.promises;

// Setting ttl as true
const options={
   ttl:true,
};

// Calling dnsPromises.resolveNs() method
// asynchronously
(async function() {

   const records = await dnsPromises.resolveNs('google.com', options);

   // Printing records
   console.log(records);
})();

Output

C:\home
ode>> node resolveNs.js [  'ns3.google.com',    'ns2.google.com',    'ns4.google.com',    'ns1.google.com' ]

Updated on: 17-Jan-2022

89 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements