Node.js – dns.resolve4() Method


The dns.resolve4() method uses the DNS protocol to resolve a IPv4 address for the hostname. The arguments passed to the callback function can contain an array of multiple addresses.

Syntax

dns.resolve4(hostname, [options], callback)

Parameters

  • hostname - This parameter takes input for the hostname to be resolved.

  • options - It can have the following options

    • ttl - It defines the Time-To-Live (TTL) for each record. Callback receives an array of addresses like this

{ address: '1.2.3.4', ttl:60 }
  • callback - It will catch errors, if any.

Example 1

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

// dns.resolve4() Demo Example

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

// Passing a single dns to get values
dns.resolve4('tutorialspoint.com', (err,address) => console.log('address: %j', address));

Output

It will produce the following output −

address: ["157.90.94.102"]

Example 2

Let's take another example

// dns.resolve4() Demo Example

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

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

// Passing the dns address below
dns.resolve4('tutorialspoint.com', options, (err, address) => console.log('address: %j', address));

Output

address: [{"address":"157.90.94.102","ttl":10000}]

Updated on: 24-Nov-2021

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements