Using the DNS module in Node.js

Posted by: Mahesh Sabnis , on 10/26/2015, in Category Node.js
Views: 10859
Abstract: Use the Node.js dns module to do DNS lookup, as well as to use underlying OS name resolution abilities.

Domain Name System (DNS) Servers matches domain names with their associates IP Addresses. This is a hierarchical distributed naming system for resources (e.g. Computers, Sites, Services, etc.), connected to internet. In server-side programming languages, we have APIs for working with DNS. In Node.js, we have the DNS module which provides functions that uses underlying operating system facilities to resolve and look up DNS names. If we want to access an IP address of the remote server, we can make use of the DNS Module.

 

In this article, we will use the DNS module to look up and resolve domain names.

You can use any IDE of your choice like Sublime Text or the free Visual Studio Code. If you are using Visual Studio Code, you also need NTVS (Node Tools for Visual Studio). Here’s an entire article on the same Node Tools for Visual Studio.

Note: This application is developed using Visual Studio Code. This is a new editor by Microsoft for building and debugging modern web and cloud applications.

DNS Module

This module is used to do an actual DNS lookup as well as to use name resolution functionalities of the underlying Operating System. This module provides a asynchronous network wrapper and can be imported using the following syntax.

var dnsServer = require('dns'); 

Step 1: Create a new folder of name VSCodeDNSServer. Open Visual Studio Code, then open this folder using File > Open Folder, options. Create a new sub folder in VSCodeDNSServer folder of the name Scripts, as shown in the following image:

vscode-new-folder

Step 2: To install the Node.js intellisense for the application, open Node.js Command prompt and from this command prompt, navigate to the VSCodeDNSServer folder and run the following Commands

npm install -g tsd

This will install the TypeScript Definition for the application.

Run the following command from the Command Prompt to enable intellisense of Node.js for the current project.

tsd query node --action install

Step 3: In the Scripts folder, add a new file of name app.js and add the following code in it.

//1.
var dns = require('dns');

//2.
var params = process.argv.splice(2);
//3.
var serverName = params[0];

//4.
function discoverAddressLookupReverse(operationType, tcpip) {

    dns.reverse(tcpip, function (err, domainNames) {
        if (err) {
            console.log(err);
        } else
            if (domainNames.length > 1) {

                console.log(operationType + ' domain names for the address ' + tcpip + ' for server ' + serverName)
            } else {
                console.log(operationType + ' domain name for the address' + tcpip + ' for server ' + serverName);

            }
    });
}

//5.
dns.resolve(serverName, function (err, addr) {

    if (err) {
        console.log('Something is Wrong ' + err.message);
        return;
    }
    discoverAddressLookupReverse('resolve', addr.toString());

});

//6.

dns.lookup(serverName, function (err, addr, family) {
    if (err) {
        console.log('Something is Wrong ' + err.message);
        return;
    }
    discoverAddressLookupReverse('lookup', addr.toString());
});

The above code has the following specifications (Note: Look up the numbers in the comments)

1. Load the DNS module which is used to lookup and resolve the Domain Names.

2. Read the domain name entered on the command prompt e.g. Node app <domain name>, e.g. google.com, the domain name is on index 2 on the command prompt

3. Reads the domain server name.

4. The functions discoverAddressLookupReverse () accepts two parameters, the first represents the operation type e.g. Resolve or Lookup. The second parameter, represents the address information of the domain. This function uses the reserve () function of the dns module and reverse resolves ip address to an array of host names. The discoverAddressLookupReverse (), finally prints the resolve and lookup of the domain.

5. Resolves the host name.

6. Resolve the host name to the first found IPv4 or IPv6 address.

Step 4: Right Click on the app.js and select options Open in Command Prompt, this opens the command prompt where run the following command.

Node app www.google.com

The result will be as shown below

nodejs-dn-server

Conclusion

We saw how the Node.js dns module is used to do DNS lookup, as well as to use underlying operating system name resolution abilities. 

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Mahesh Sabnis is a DotNetCurry author and Microsoft MVP having over 17 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). Follow him on twitter @maheshdotnet


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook