In Node.js, The "DNS module" in Node.js provides functions to perform DNS "Domain Name System" lookups and resolve DNS-related tasks.
This module is particularly useful when working with networked applications that need to interact with domain names.
Domain Verification: We can use DNS Modules `lookup` methods to verify our domain ownership.
Caching: We can cache resolved DNS records to increase performance and reduce the load on DNS servers.
Network Configuration: We can use DNS Module for configuring network-related services like load balancers or proxy servers.
Here's an overview of methods provided by the DNS module:
Resolves a hostname (e.g., a domain name) into the first found IP address. It uses the operating system's DNS resolution mechanism.
const dns = require('dns'); dns.lookup('www.domainName.com', ( err, address, family ) => { // Handling Error if (err) throw err; console.log( `Fetch Address: ${address}, family: IPv${family}` ); });
Address: 71.71.12.103, family: IPv4
Resolves a hostname into an array of records based on the specified record type `rrtype`. Common record types include 'A' (IPv4 addresses), 'AAAA' (IPv6 addresses), 'MX' (mail exchange), etc.
const dns = require('dns'); dns.resolve( 'www.domainName.com' , 'A', ( err, records ) => { // Handling Error if (err) throw err; console.log(' Fetch IPv4 records: ', records ); });
IPv4 addresses: [ '71.71.12.103' ]
Resolves the text `TXT` records of a hostname.
const dns = require('dns'); dns.resolveTxt( 'domainName.com', ( err, records ) => { // Handling Error if (err) throw err; console.log( 'DNS Txt Records: ' , records ); });
Resolves the canonical name `CNAME` of a hostname.
const dns = require('dns'); dns.resolveCname( 'www.domainName.com' , ( err , records ) => { // Handling Error if (err) throw err; console.log( 'DNS CNAME Records Fetch: ', records ); });
Resolves the name server `NS` records of a hostname.
const dns = require('dns'); dns.resolveNs( 'domainName.com' , ( err, records ) => { // Handling Error if (err) throw err; console.log( 'DNS NS Records Fetch: ', records ); });