Sunday, April 19, 2020

Node.js dns Module

Node.js dns Module

nodejs dns module


The dns module is named upon Domain Name System(DNS) and provides a mechanism for name resolution. Underlying operating system facilities are used by dns.lookup() method for name resolution. It may not require performing any network operation itself for name resolution. For example, we can use this module to look up the IP addresses of the given hostnames.

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

This method resolves a hostname (e.g. 'example.com') into the IPv4 or IPv6 record with the help of underlying operating system facilities. We do not need to keep a record of IP addresses manually. The DNS servers provide an excellent solution of converting domain or subdomain names to their respective IP addresses.

const dns = require('dns');

const website = 'programminghunk.com';
// lookup function
dns.lookup(website, (err, address, family) => {
console.log('The address of %s is %j family: IPv%s',
           website, address, family);
});
/*
Output:
The address of programminghunk.com is "216.239.32.21" family: IPv4
*/ 

All remaining functions in dns module need to be connected to the actual DNS server because the use of the network is required to execute the DNS server. When, we do not want to use the underlying operating system, and we want to execute DNS queries on the actual DNS server these functions can be used.

dns.lookupService(address, port, callback)

This method resolves the specified address and port into a hostname and service using getnameinfo().
               

dns.resolve(hostname[, rrtype], callback)

This method resolves a hostname (e.g. 'example.com') into an array of the record types specified by rrtype (resource record type, default is 'A' for IPv4, 'AAAA' for IPv6, 'MX'  for mail exchange records, 'TXT' for text records etc.).


var dns = require('dns');
dns.resolve('www.google.com','A', function onLookup(err, address) {
     if(err)
           throw err;
    
   console.log('address->', address);

});
/*
Output:
address-> [ '172.217.167.228' ]
*/ 

dns.resolve4(hostname, callback)

This methods works same as dns.resolve(), but only for IPv4 queries. The method returns an array of IPv4 addresses (such as ['114.15.79.11', '11.15.79.13', '114.15.79.16']).
               

dns.resolve6(hostname, callback)

This method looks up for IPv6 queries
               

dns.resolveMx(hostname, callback)

The same as dns.resolve(), but it is used for mail exchange queries or the MX records only.

dns.resolveTxt(hostname, callback)

The method works same as dns.resolve() except the text queries or TXT records are resolved.

dns.resolveNs(hostname, callback)

The method works the same as dns.resolve() method except it works only for name server records (NS records).

dns.resolveCname(hostname, callback)

The method works like dns.resolve(), except it lookup for canonical name records or the CNAME records. These addresses are returned as an array of the CNAME records available for the given hostname.

dns.reverse(ip, callback)

This method reverses an IP address into an array of hostnames.

var dns = require('dns');

dns.lookup('www.google.com', function onLookup(err, address, family) {
   console.log('address->', address);
   dns.reverse(address, function (err, hostnames) {
      if (err) {
         console.log(err.stack);
      }

      console.log('The reverse for ' + address + 'is : ' + JSON.stringify(hostnames));
   }); 
});
/*
Output:
address: 172.217.160.228
reverse for 172.217.160.228: ["del03s09-in-f4.1e100.net"]
*/ 

dns.resolveSoa(hostname, callback)      

This method looks up a start of authority record for the specified hostname.

dns.getServers()

This method returns an array containing IP addresses as strings, currently being used to be resolved.

var dns = require('dns');
console.log(dns.getServers());
/*
Output:

[ '192.168.107.1', '8.8.8.8' ]
*/ 
              

dns.setServers(servers)

This method sets the IP addresses of the servers