Open In App

Node.js dns.resolveTxt() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The dns.resolveTxt() method is an inbuilt application programming interface of the dns module which is used to resolve TXT or text queries records for the specified hostname using DNS protocol.

Syntax:

dns.resolveTxt( hostname, callback )

Parameters: This method has two parameters as mentioned above and described below:

  • hostname: This parameter specifies a string which denotes the hostname to be resolved.
  • callback: It specifies a function to be called after DNS resolution of the hostnames.
    • error: It specifies error if generated.
    • addresses: It is an array of string that signifies the returned text queries records for the hostname.

Return Value: This method returns error, addresses through callback function. These data are passed as parameters to the callback function.

Below examples illustrate the use of dns.resolveTxt() method in Node.js:

Example 1:




// Node.js program to demonstrate the   
// dns.resolveTxt() method
  
// Accessing dns module
const dns = require('dns');
  
// Calling dns.resolveTxt() method for hostname
// geeksforgeeks.org and displaying them in
// console as a callback
dns.resolveTxt('geeksforgeeks.org', (err, 
    addresses) => console.log('TXT records: %j', addresses));


Output:

TXT records: [
    ["fob1m1abcdp777bf2ncvnjm08n"],
    ["v=spf1 include:amazonses.com include:_spf.google.com -all"]
]

Example 2:




// Node.js program to demonstrate the   
// dns.resolveTxt() method
  
// Accessing dns module
const dns = require('dns');
  
// Calling dns.resolveTxt() method for
// hostname google.com and displaying
// them in console as a callback
dns.resolveTxt('google.com', (err, 
    addresses) => console.log('TXT records: %j', addresses));


Output:

TXT records: [
    ["globalsign-smime-dv=CDYX+XFHUw2wml6/Gb8+59BsH31KzUr6c1l2BPvqKX8="],
    ["v=spf1 include:_spf.google.com ~all"],
    ["docusign=1b0a6754-49b1-4db5-8540-d2c12664b289"],
    ["facebook-domain-verification=22rm551cu4k0ab0bxsw536tlds4h95"],
    ["docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e"]
]

Note: The above program will compile and run by using the node index.js command.

Reference: https://nodejs.org/api/dns.html#dns_dns_resolvetxt_hostname_callback



Last Updated : 13 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads