Node.js - dns.resolveCname() Method

Node.jsServer Side ProgrammingProgramming

The dns.resolveCname() method uses the DNS protocol to resolve CNAME records for the hostname. The addresses argument passed to the callback function will contain an array of canonical records in an array.

Syntax

dns.resolveCname(hostname, callback)

Parameters

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

  • callback – This function will catch errors, if any.

    • addresses – Returns CNAME addresses for the hostname.

Example 1

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

// dns.resolveCname() Demo Example

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

// Passing the argument below
dns.resolveCname('tutorialspoint.com', (err, addresses) => console.log('addresses: %j', addresses));

Output

C:\home\node>> node resolveCname.js
QueryReqWrap {
   bindingName: 'queryCname',
   callback: [Function],
   hostname: 'tutorialspoint.com',
   oncomplete: [Function: onresolve],
   ttl: false,
   channel: ChannelWrap {} }
addresses: undefined

Example 2

// dns.resolveCname() Demo Example

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

// Passing the argument below
dns.resolveCname('localhost', (err, addresses) => console.log('addresses: %j', addresses));

Output

C:\home\node>> node resolveCname.js
addresses: undefined
raja
Published on 17-Jan-2022 12:37:59
Advertisements