Using the DNS module in Node.js

Posted by: Mahesh Sabnis , on 10/26/2015, in Category Node.js
Views: 17057
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:


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.

var dns = require('dns');

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

function discoverAddressLookupReverse(operationType, tcpip) {

    dns.reverse(tcpip, function (err, domainNames) {
        if (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);


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

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



dns.lookup(serverName, function (err, addr, family) {
    if (err) {
        console.log('Something is Wrong ' + err.message);
    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., 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

The result will be as shown below



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. 

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

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

Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades 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), and Front-end technologies like Angular and React. Follow him on twitter @maheshdotnet or connect with him on LinkedIn

Page copy protected against web site content infringement 	by Copyscape

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