Updated March 28, 2023
Introduction to Node.JS DNS
Node.JS DNS is a module used for actual DNS lookup and to use underlying operating system name resolution. Domain Name System(DNS) does not always use DNS protocol for lookups. Main advantage of DNS is user need not memorize all the IP addresses as DNS provide a good solution to convert domain or subdomain names to IP addresses. This Node.JS DNS contains various methods to get information of the hostname.
Syntax
var dns = require('dns');
Methods of Node.JS DNS
All the other functions in DNS module connect to actual DNS server to perform name resolution, these methods have different set of configuration files. Let us look at different DNS methods.
dns.lookup(hostname[, options], callback)
Looks up a hostname, callback function contains the information about the hostname along with its IP address.
dns.lookupService(address, port, callback)
Looks up address and port number, callback function contains the information about the hostname along with its IP address. Using getnameinfo service, given address and port are resolved to hostname.
dns.resolve(hostname[, rrtype], callback)
Resolves and returns an array of records which belongs to specific hostname For e.g. www.gmail.com
Here, rrtype specifies resource record type, default value set to ‘A’, list of records are as below,
A: IPv4 address
AAAA: IPv6 address
ANY: any records
CNAME: canonical name records
NAPTR: name authority pointer records
MX: mail exchange records
PTR: pointer records
NS: name server records
SOA: start of authority records
SRV: service records
TXT: text records
Callback specifies a function to be called after DNS resolution
dns.resolve4(hostname, callback)
Looks up for IPv4 address, similar to dns.resolve including an array of IPv4 addresses
dns.resolve6(hostname, callback)
Looks up for IPv6 address, similar to dns.resolve including an array of IPv6 addresses.
dns.resolveMx(hostname, callback)
Similar to dns.resolve, which resolves mail exchange records for specific hostname.
dns.resolveTxt(hostname, callback)
Similar to dns.resolve, which resolves text query records for specific hostname.
dns.resolveSrv(hostname, callback)
Similar to dns.resolve, which resolves service records for specific hostname.
dns.resolveSoa(hostname, callback)
Similar to dns.resolve, which resolves start of authority records for specific hostname.
dns.resolveNs(hostname, callback)
Similar to dns.resolve, which resolves name server records for specific hostname.
dns.resolveCname(hostname, callback)
Similar to dns.resolve, which resolves canonical name records for specific hostname.
dns.reverse(ip, callback)
Reverses an IP address into an array of hostnames.
dns.getServer()
Returns an array containing all IP addresses belonging to current server.
dns.setServer(servers)
Sets the IP addresses of the servers.
Examples to Node.JS DNS
Let us look at a simple example and retrieve the IP address of the website.
Example #1
Code:
var dns = require('dns');
var demo = dns.lookup('www.educba.com', function (err, addresses, family) {
console.log(addresses)
});
Output:
Here, we include ‘dns’ module and create an object. Call a lookup function of dns to display the IP address of the website ‘www.educba.com’
Example #2
Code:
var dns = require('dns');
var demo = dns.lookup('www.educba.com', function (err, address, family) {
console.log(address);
dns.reverse(address, function (err, hostnames) {
if(err) {
console.log(err.stack);
}
console.log(address + ': ' + JSON.stringify(hostnames));
});
});
Output:
Here, also we have included ‘dns’ module and object is created, with lookup function, we also call reverse function which reverses the IP address.
Example #3
Code:
var dns = require('dns');
var demo = dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
console.log(hostname, service);
});
Output:
Example #4
Code:
const { Resolver } = require('dns');
const resolver = new Resolver();
resolver.setServers(['127.0.0.0']);
console.log(resolver.getServers());
Output:
Example #5
Code:
const dns = require('dns');
const rrtype="A";
dns.resolve('www.gmail.com', rrtype, (err,
records) => console.log('records: %j', records));
const rrtype1="TXT";
dns.resolve('www.gmail.com', rrtype1, (err,
records) => console.log('records: %j', records));
const rrtype2="NS";
dns.resolve('www.gmail.com', rrtype2, (err,
records) => console.log('records: %j', records));
Output:
There are some error codes DNS methods might return,
- NOTFOUND: If domain is not found
- BADQUERY: DNS query is not formatted properly
- TIMEOUT: if there is a timeout while connecting to dns servers
- FILE: if there is an error in reading file
- EOF: end of file
- CANCELLED: if dns query is cancelled
- NOMEM: if there is no memory
and many more.
Conclusion
We have seen what is Node.JS DNS and its advantage, it is a hierarchical distributed naming system connected to internet. We have seen how we can access the IP address of the server. We have also investigated some of the examples using DNS methods like lookup, reverse, resolve, etc. and also seen some of the error codes which DNS returns.
Recommended Articles
This is a guide to Node.JS DNS. Here we discuss Syntax to Node.JS DNS, methods to implement it, resolves and returns an array of records, examples with codes outputs. You can also go through our other related articles to learn more –