Friday, March 17, 2023
HomeNodejsNodeJS DNS Module: A Newbie's Information

NodeJS DNS Module: A Newbie’s Information


Node.js DNS is used to get data relating to the DNS by doing a DNS lookup. 

DNS Lookup means trying on the DNS data for a site or an IP tackle of a server. These DNS data could be the listing of various IP addresses, MX data, Title Server data, or providers related to a selected area. Not solely this there are numerous functionalities supplied by the DNS module, which we are going to perceive on this article.

What’s DNS?

Earlier than going additional let’s attempt to perceive the idea of DNS for higher understanding. Every system connect with the web has its tackle which known as an IP tackle. DNS stands for Area Title System used to transform the hostname into an IP tackle.

Additionally learn: NodeJS Command Line Choices

The NodeJS DNS Module

Node.js DNS can be utilized by importing the DNS modules utilizing the next assertion.

Syntax:

const dns = require('dns'); 

DNS Module Strategies

The DNS module offers varied strategies let’s perceive them one after the other.

lookup()

This technique of DNS module is used to search out the IP tackle of a site identify.

Syntax:

dns.lookup('area', (err, end result) => { 
    // operate physique
}) 

This technique takes two arguments, a site to search out its IP tackle, and a callback operate, during which we are able to print the error or the IP of the entered area.

The lookup technique is used a pc inside system to get the IP tackle of the entered area.

Instance:

const dns = require('dns'); 

dns.lookup('askpython.com', (err, end result) => { 
    if(err) { 
        console.log(err); 
    } else {
        console.log(end result); 
    }
}) 

Let’s use this operate to search out the IP tackle of “askpython.com”, and contained in the callback operate first test for the error if the error happens print it within the console, if not then print the returned IP tackle.

Output:

Lookup Example

loopupService()

This technique takes three augments, first is the IP tackle, second is the Port and third is a callback operate that returns the host predominant and repair related to it.

Syntax:

dns.lookupService('area', port, (err, host, service) => { 
    // operate physique
}) 

resolve()

The resolve technique can be used to get the IP tackle of an entered area, however as a substitute of utilizing the pc’s inside system identical to the lookup technique to search out the IP, it makes a community request to the entered area identify after which finds all of the IP tackle hyperlink with that area.

Syntax:

dns.resolve('area', (err, end result) => { 
    // operate physique
}) 

Instance:

Let’s use the identical instance to see the distinction between the output of the lookup technique and the resolve technique.

const dns = require('dns'); 

dns.resolve('askpython.com', (err, end result) => { 
    if(err) { 
        console.log(err); 
    } else {
        console.log(end result); 
    }
}) 

Output:

Resolve Example

There are numerous varieties of resolve technique which work equally, however returns the array with a sure kind of IP tackle. A few of them are – 

This technique returns an array of IPv4 IP addresses which is the older model of the IP tackle. IPv4 is developed within the 90s and has 4 billons addresses utilized by the vast majority of web sites.

This technique returns an array of IPv6 IP addresses which is the newer model of the IP tackle. For the reason that variety of IPv4 addresses is proscribed, IPv6 is created which is utilized by new web sites.

This technique returns an array of MX data related to the entered area. MX is a Mail Alternate report.

This technique returns an array of NS data. NS stands for Title Server, you could hear this phrase when linking a site to a internet hosting server.

reverse()

That is simply the alternative of the lookup() and resolve() technique, this technique takes the IP tackle as an argument and returns the area related to that IP tackle.

Syntax:

dns.reverse('ip-address', (err, end result) => { 
    // operate physique
}) 

Instance:

Let’s use this technique and put the google public IP tackle, to see the end result.

const dns = require('dns'); 

dns.reverse('8.8.8.8', (err, end result) => { 
    if(err) { 
        console.log(err); 
    } else {
        console.log(end result); 
    }
}) 

Output:

Reverse Example

Abstract

Node.js DNS offers many helpful strategies to get data relating to the area and the IP tackle. Through the use of it you could find the IP tackle related to a site, and you too can discover the providers related to a selected IP. It additionally helps find the Title Server and Mail Exchanges report of a site. Hope this text lets you perceive the DNS module of Node.js.

Reference

https://nodejs.org/api/dns.html#dns

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments