a blog for those who code

Wednesday 30 July 2014

Lets take a look at DNS module in Nodejs

In this post we will look at the DNS module in Nodejs. DNS module will provide you to refer things by their domain names instead of their IP addresses. Node.js DNS module is used to actual DNS lookup as well as to use underlying operating system name resolution functionalities.

DNS module provides a asynchronous network wrapper and can be imported using he following syntax var dns = require('dns');

The dns module consists of two main methods resolve() and reverse().

resolve() - Turns a domain name into a DNS record
reverse() - Turns an IP address into an array of domain names

dns.resolve() takes three arguments :

First Argument : The domain to be resolved
Second Argument : rrtypes which are ('A', 'AAAA', 'MX', 'TXT', 'SRV', 'PTR', 'NS' and 'CNAME')
Third Argument : It returns the response from the DNS server.

dns.resolve('codingdefined.com', 'A', function(err,rec) {
if(err) {
console.log(err);
}
console.log(rec);
});

The module also provides you with methods like dns.resolve4 (for IPv4 queries), dns.resolve6 (for IPv6 queries) and dns.resolveMx (for mail exchange queries).

dns.resolve4('codingdefined.com', callback) instead of dns.resolve('codingdefined.com', 'A', callback)

dns.resolve6('codingdefined.com', callback) instead of dns.resolve('codingdefined.com', 'AAAA', callback)

dns.resolveMx('codingdefined.com', callback) instead of dns.resolve('codingdefined.com', 'MX', callback)

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment