Using the dns Module

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.

Image
Image

Table 10.2 Methods that can be called on the dns module

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.

Listing 10.3 dns_lookup.js: Performing lookups and reverse lookups on domains and IP addresses


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 });


Image

Figure 10.3 Output of performing lookups and DNS names when performing reverse lookups.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.43.26