If you want a Node.js application to resolve DNS domain names, look up domains, or do reverse lookups, then you will find the dns
module very helpful. A DNS lookup contacts the domain name server and requests records about a specific domain name. A reverse lookup contacts the domain name server and requests the DNS name associated with an IP address. The dns
module provides functionality for most of the lookups you may need to perform. Table 10.2 lists the methods, their syntax, and what they do.
Listing 10.3 shows how to perform lookups and reverse lookups. In line 3, resolve4()
looks up the IPv4 addresses, and then in lines 5–8, reverse()
is called on those same addresses and performs the reverse lookup. The output is shown in Figure 10.3.
01 var dns = require('dns'),
02 console.log("Resolving www.google.com . . .");
03 dns.resolve4('www.google.com', function (err, addresses) {
04 console.log('IPv4 addresses: ' + JSON.stringify(addresses, false, ' '));
05 addresses.forEach(function (addr) {
06 dns.reverse(addr, function (err, domains) {
07 console.log('Reverse for ' + addr + ': ' + JSON.stringify(domains));
08 });
09 });
10 });