Node.js - dnsPromises.getServers() Method


The dnsPromises.getServers() method returns an array of IP address strings, formatted across RFC 5952. These IP strings are currently configured for DNS configurations. The string will include a port only if a custom port is used.

Syntax

dnsPromises.getServers( )

Example 1

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

// dnsPromises.getServers() Demo Example

// Importing promises from dns module
const { Resolver } = require('dns').promises;

// Initializing resolver constructor
const dnsPromises = new Resolver();

// Creating async function
(async function() {
   // Getting addresses
   const addresses = await dnsPromises.getServers();

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

Output

C:\home
ode>> node getServers.js [ '127.0.0.53' ]

Example 2

// dnsPromises.getServers() Demo Example

// Importing promises from dns module
const { Resolver } = require('dns').promises;

// Initializing resolver constructor
const dnsPromises = new Resolver();

// Creating async function
(async function() {
   // Getting addresses
   const addresses = await dnsPromises.getServers();

   // Printing each address one by one
   addresses.forEach(element => {
      console.log(element);
   });
})();

Output

C:\home
ode>> node getServers.js '4.4.4.4', '2001:4860:4860::8888', '4.4.4.4:1053', '[2001:4860:4860::8888]:1053',

Updated on: 17-Jan-2022

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements