Node.js DNS
DNS is a node module used to do name resolution facility which is provided by the operating system as well as used to do an actual DNS lookup.
Advantage: No need for memorizing IP addresses – DNS servers provide a nifty solution for converting domain or subdomain names to IP addresses.
Example 1: In this example, we will print the address of w3wiki on the console.
javascript
// Include 'dns' module and create its object const dns = require( 'dns' ); const website = 'w3wiki.net' ; // Call to lookup function of dns dns.lookup(website, (err, address, family) =>; { console.log( 'address of %s is %j family: IPv%s' , website, address, family); }); // Execute using $ node <filename>; |
Output:
address of w3wiki.net is "52.25.109.230" family: IPv4
Example 2: In this example, we will print the address of the w3wiki.
javascript
// Include 'dns' module and create its object const dns = require( 'dns' ); // Call to reverse function along with lookup function. dns.lookup( 'www.w3wiki.net' , function onLookup(err, address, family) { console.log( 'address:' , address); dns.reverse(address, function (err, hostnames) { console.log( 'reverse for ' + address + ': ' + JSON.stringify(hostnames)); }); }); // Execute using $ node <filename>; |
Output:
address: 52.222.176.140 reverse for 52.222.176.140: ["server-52-222-176-140.bom52.r.cloudfront.net"]
Reference: https://nodejs.org/docs/latest-v9.x/api/dns.html#dns_dns