Node.js - dns.reverse() Method


The dns.reverse() method is somewhat opposite of the method dns.lookup().This method performs a reverse DNS query that resolves an IPv4 or IPv6 to an array of host names.

Syntax

dns.reverse(ip, callback)

Parameters

  • ip – This takes an input for ip as a string, for which the DNS needs to be found.

  • callback – It will catch errors, if any.

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() method Demo Example

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

// It will return the dns name
dns.reverse('127.0.0.53', (err, addresses) =>
   console.log('addresses: %j', addresses));

Output

C:\home
ode>> node reverse.js addresses: ["localhost"]

Example 2

// dns.reverse() method Demo Example

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

// It will return the dns name
dns.reverse('127.0.0.1', (err, addresses) =>
console.log('addresses: %j', addresses));

// It will return the dns name
dns.reverse('95.217.74.146', (err, addresses) =>
   console.log('addresses: %j', addresses));

Output

C:\home
ode>> node reverse.js addresses: [] addresses: ["static.146.74.217.95.clients.your-server.de"]

Updated on: 17-Jan-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements