Nodejs | DNS

Introduction:

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 memorising IP addresses – DNS servers provide a nifty solution of converting domain or subdomain names to IP addresses.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Include 'dns' module and create its object
const dns = require('dns');
  
const website = 'geeksforgeeks.org';
// 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>

chevron_right


Output:

address of geeksforgeeks.org is "52.25.109.230" family: IPv4

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Include 'dns' module and create its object
var dns = require('dns');
  
// Call to reverse function along with lookup function.
dns.lookup('www.geeksforgeeks.org'
     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>

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

About immukul

Executive Software Developer at Cavisson Systems