Node.js – dnsPromises.reverse() Method

Node.jsServer Side ProgrammingProgramming

The dnsPromises.reverse() method performs a reverse DNS search to resolve the IPv4 or Ipv6 address to an array of hostnames. The promise is rejected with an Error object if a Success status is not encountered.

Syntax

dnsPromises.reverse( ip )

where, ip is the parameter that takes the input for the IP address to be resolved.

Example 1

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

// dns.reverse() Demo Example

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

const dnsPromises = dns.promises;

// Passing IP to find the DNS name
dnsPromises.reverse('95.217.74.146').then((response) => {
   console.log("Resolved DNS is:", response);
})

Output

C:\home\node>> node reverse.js
Resolved DNS is: ['static.146.74.217.95.clients.your-server.de']

Example 2

// dns.reverse() Demo Example

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

const dnsPromises = dns.promises;

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

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

   const records = await dnsPromises.reverse('127.0.0.8', options);

   // Printing records
   console.log("DNS: ", records);
})();

Output

C:\home\node>> node reverse.js
DNS: [ 'localhost' ]
raja
Published on 17-Jan-2022 11:34:29

Advertisements