Learn How to Build DNS Module Utility Modules in Nodejs

0
3953
DNS Module

In this article, we are going to discuss building the Node.js DNS module. A DNS module is used to do the DNS lookup and underlying operating system name resolution functionalities through an asynchronous network wrapper. We use the following syntax to import an asynchronous network wrapper.

Methods present in the DNS Module of Node.Js
The following are the basic methods and their descriptions which are present in the DNS Module of Node.js.

S No

Method

Description

1.

dns.lookup(hostname[, options], callback)

This method is used to resolve a hostname (e.g. ‘www.eduonix.com’) into A (IPv4) default or AAAA (IPv6) address record. Options can be an object or integer. If options is an integer, then it must be 4 or 6 for IPv4 or IPv6 respectively.

2.

dns.lookupService(address, port, callback)

This method is used to resolve the given address and port into a hostname and service using the getnameinfo.

3.

dns.resolve(hostname[, rrtype], callback)

This method is used to resolve a hostname (e.g. ‘www.eduonix.com’) into an array of the record types specified by rrtype.

4.

dns.resolve4(hostname, callback)

This method is same as dns.resolve () method but only for IPv4 queries (A records). Addresses is an array of IPv4 addresses (e.g. [‘ 67.227.229.33’, etc.]).

5.

dns.resolve6(hostname, callback)

This method is same as dns.resolve4 () except for IPv6 queries (an AAAA query).

6.

dns.resolveMx (hostname, callback)

This method is same as dns.resolve (), but only for mail exchange queries (MX records).

7.

dns.resolveTxt (hostname, callback)

This method is same as dns.resolve (), but only for text queries (TXT records).

8.

dns.resolveSrv (hostname, callback)

This method is same as dns.resolve (), but only for service records (SRV records).

9.

dns.resolveSoa(hostname, callback)

This method is same as dns.resolve (), but only for start of authority record queries (SOA record).

10.

dns.resolveNs (hostname, callback)

This method is same as dns.resolve (), but only for name server records (NS records).

11

dns.resolveCname (hostname, callback)

This method is same as dns.resolve (), but only for canonical name records (CNAME records).

12

dns.reverse (ip, callback)

This method is used to resolve an ip address to an array of hostnames.

13

dns.getServers ()

This method is used to return an array of IP addresses as strings that are currently being used for resolution.

14

dns.setServers (servers)

For the given array of IP addresses as strings, this method is used to set them as the servers in order to use for resolving.

‘dns.resolve ()’ method rrtypes:
The following are the rrtypes used by ‘dns.resolve ()’ method:

S No.

rrtypes

Description

1.

A

This rrtype is used for the IPV4 addresses, which are default.

2.

AAAA

This rrtype is used for the IPV6 addresses.

3.

CNAME

This rrtype is used for the canonical name records.

4.

MX

This rrtype is used for the mail exchange records.

5.

NS

This rrtype is used for the name server records.

6.

PTR

This rrtype is used for the reverse IP lookups.

7.

SOA

This rrtype is used for the start of authority record.

8.

SRV

This rrtype is used for the SRV records.

9.

TXT

This rrtype is used for the text records.

Error Codes returned by DNS query
The following are the error codes returned by the DNS Query.

S No.

Error Codes

Description

1.

dns.NODATA

This error code is returned by the DNS server when there is no data.

2.

dns.FORMERR

This error code is returned by the DNS server when query was badly formatted.

3.

dns.SERVFAIL

This error code is returned by the DNS server when there is general failure.

4.

dns.NOTFOUND

This error code is returned by the DNS server when domain name not found.

5.

dns.NOTIMP

This error code is returned by the DNS server when it does not implement requested operation.

6.

dns.REFUSED

This error code is returned by the DNS server when DNS server refused the query.

7.

dns.BADQUERY

This error code is returned by the DNS server when there is a badly formatted DNS query.

8.

dns.BADNAME

This error code is returned by the DNS server when there is a badly formatted hostname.

9.

dns.BADFAMILY

This error code is returned by the DNS server when unsupported address family.

10.

dns.BADRESP

This error code is returned by the DNS server when there is a badly formatted DNS reply.

11.

dns.CONNREFUSED

This error code is returned by the DNS server when it could not contact the DNS servers.

12.

dns.TIMEOUT

Timeout while contacting DNS servers.

13.

dns.EOF

This error code is returned by the DNS server at the end of a file.

14.

dns.FILE

This error code is returned by the DNS server when there is an error reading the file.

15.

dns.NOMEM

This error code is returned by the DNS server when it is out of memory.

16.

dns.DESTRUCTION

This error code is returned by the DNS server when the channel is being destroyed.

17.

dns.BADSTR

This error code is returned by the DNS server when there is a badly formatted string.

18.

dns.BADFLAGS

This error code is returned by the DNS server when the Illegal flags are specified.

19.

dns.NONAME

This error code is returned by the DNS server when the given hostname is not numeric.

20.

dns.BADHINTS

This error code is returned by the DNS server when Illegal hints flags are specified.

21.

dns.NOTINITIALIZED

This error code is returned by the DNS server when c-ares library initialization has not yet been performed.

22.

dns.LOADIPHLPAPI

This error code is returned by the DNS server when there is an error in loading iphlpapi.dll.

23.

dns.ADDRGETNETWORKPARAMS

This error code is returned by the DNS server when it could not find the GetNetworkParams function.

24.

dns.CANCELLED

This error code is returned by the DNS server when the DNS query is cancelled.

Example on use of DNS Module in Node.js Application
In the following example, we are going to demonstrate the use of the above DNS module methods. Here, we are going to create one js file named DNSModuleServer.js, which has the following code.

DNSModuleServer.js

Explanation of the code
In the above code, we are looking up ‘www.eduonix.com’ domain and retrieving IP address which are printed on the console. Next, we are using the ‘reverse’ method to print the reverse of the host name by using the method ‘JSON.stringify (hostnames))’.

Output
When we execute the above Node.js programs, then we will observe the following output on the console.

Source code for this DNS module of Node.js blog

Conclusion: –
In this article, we discussed the various methods present in the DNS module of Node.js. We have imported the DNS module and use the associated methods to build a DNS Module utility.

LEAVE A REPLY

Please enter your comment!
Please enter your name here