Node.js Tutorial – Domain Name System (DNS)

The Tutorial provides the detailing about the node.js DNS module which provides names resolution or look up IP addresses of the host names. The node.js DNS module does not always use the DNS Prootocl for lookup. In case of specific need to perform name resolution using network communications, it is suggested to invoke the method dns.lookup().

const dns = require('dns');
dns.lookup('oracleappshelp.com', (err, address, family) => {
  console.log('address: %j family: IPv%s', address, family);
});

If the given lookup name is Correct, then it will provide the IP Address , else it will throw the error as given below

C:\Users\user\Desktop>node dns.js
address: undefined family: IPvundefined

The other DNS methods /functions in the dns module connect to actual DNS Server to perform the DNS name resolution and uses the network to perform the DNS queries. These methods do not use the same set of configurations as dns.lookup()

Node.js dns.resolver class

The dns.resolver is the independent class for processing the DNS requests.

Creating a new resolver uses the default server settings. Setting the servers used for a resolver using resolver.setServers() does not affect other resolvers.

const { Resolver } = require('dns');
const dnsResolver = new Resolver();
dnsResolver.setServers(['12.32.45.65']);
// This request will use the server at 12.32.45.65, independent of global settings.
dnsResolver.resolve4('oracleappshelp.com', (err, addresses) => {
  // ...
});

Node.js dns methods

The below given are the available dns methods

dns method namedns method description
dns.getServers() Returns an array containing IP addresses related to the current server
dns.setServers(servers) Sets the IP addresses of the servers
dns.lookup(hostname[, options], callback) Looks up a hostname. A callback function contains information about the hostname, including it’s IP address
dns.lookupService(address, port, callback) Looks up a address and port. A callback function contains information about the address, such as the hostname
dns.resolve(hostname[, rrtype], callback) Returns an array of record types belonging to the specified hostname
dns.resolve4(hostname, callback) Looks up an IPv4 address. The callback function includes an array of IPv4 addresses
dns.resolve6(hostname, callback) Looks up an IPv6 address. The callback function includes an array of IPv6 addresses
dns.resolveCname(hostname, callback) Looks up CNAME records for the specified hostname. The callback function includes an array of available domains for the hostname
dns.resolveMx(hostname, callback) Looks up mail exchange records for the hostname
dns.resolveNs(hostname, callback) Looks up name server records for the hostname
dns.resolveSoa(hostname, callback) Looks up a start of authority record for the hostname
dns.resolveSrv(hostname, callback) Looks up service records for the hostname
dns.resolvePtr(hostname, callback) Looks up pointer records for the hostname
dns.resolveTxt(hostname, callback) Looks up text query records for the hostname
dns.reverse(ip, callback) Reverses an IP address into an array of hostnames
DNS methods in Node.js