Node.js – dns.getServers() Method


The dns.getServers() method returns an array of IP address strings. The address will be formatted as per the RFC 5952 standard, that are configured for DNS resolution. The string will also include the port section if a custom port is used.

Syntax

dns.getServers()

Parameters

Since it returns the list of the servers, it does not need any parameters.

Example 1

Create a file "getServers.js" and copy the following code snippet. After creating the file, use the command "node getServers.js" to run this code.

// dns.getServers() Node js Example

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

// Reading the IP related info
// for the current host
console.log(dns.getServers());

Output

[ '213.133.98.98', '213.133.100.100', '213.133.99.99' ]

Example 2

// dns.getServers() Node js Example

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

// Reading the IP related info
// for all hosts
allServers = dns.getServers();

// Iterating over all the IP's and printing them
allServers.forEach(element => {
   console.log(element);
});

Output

213.133.98.98
213.133.100.100
213.133.99.99

Updated on: 29-Oct-2021

102 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements