Node.js – dnsPromises.resolve4() Method


The dnsPromises.resolve4() method uses the DNS protocol to resolve IPv4 addresses (A records) for the hostname. A promise is resolved with an array of IP addresses when True.

The difference between the dnsPromises and dns module is that dnsPromises provides an alternative way to asynchronous DNS methods that return Promise objects instead of callbacks.

Syntax

dns.resolve4(hostname, [options])

Parameters

  • hostname – This parameter takes input for hostname to be resolved.

  • options – It can have the following options −

    • ttl – This defines the Time-To-Live (TTL) for each record. Callback receives an array of addresses like this – { address: ‘1.2.3.4’, ttl:60 }

Example 1

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

// dns.resolve4() Demo Example

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

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

Output

Resolved address is: [ '95.217.74.146' ]

Example 2

// dns.resolve4() Demo Example

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

const dnsPromises = dns.promises;

const options = {
   ttl:true,
};

// Calling resolve4 using promises
(async function() {

   // Passed dns value to be resolved
   const records = await dnsPromises.resolve4( 'tutorialspoint.com', options);

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

Output

[ { address: '95.217.74.146', ttl: 267 } ]

Updated on: 29-Oct-2021

240 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements