Easy Tutorial
❮ Nodejs Os Module Nodejs Event Loop ❯

Node.js DNS Module

Node.js Utility Module

The Node.js DNS module is used for resolving domain names. The syntax for including the DNS module is as follows:

var dns = require("dns")

Methods

No. Method & Description
1 dns.lookup(hostname[, options], callback) <br>Resolves a domain name (e.g., 'tutorialpro.org') to the first found record A (IPv4) or AAAA (IPv6). The options parameter can be an object or an integer. If no options are provided, both IPv4 and IPv6 addresses are allowed. If options is an integer, it must be 4 or 6.
2 dns.lookupService(address, port, callback) <br>Resolves the provided address and port to a domain name and service using getnameinfo.
3 dns.resolve(hostname[, rrtype], callback) <br>Resolves a domain name (e.g., 'tutorialpro.org') to an array of records of the specified type (rrtype).
4 dns.resolve4(hostname, callback) <br>Similar to dns.resolve(), but only for IPv4 (A records). Returns an array of IPv4 addresses (e.g., ['74.125.79.104', '74.125.79.105', '74.125.79.106']).
5 dns.resolve6(hostname, callback) <br>Similar to dns.resolve4(), but only for IPv6 (AAAA records).
6 dns.resolveMx(hostname, callback) <br>Similar to dns.resolve(), but only for mail exchange (MX records).
7 dns.resolveTxt(hostname, callback) <br>Similar to dns.resolve(), but only for text queries (TXT records). Returns a 2-d array of text records (e.g., [ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ]). Each sub-array contains TXT chunks of one record. They can be concatenated or used individually as needed.
8 dns.resolveSrv(hostname, callback) <br>Similar to dns.resolve(), but only for service records (SRV records). Returns an array of SRV records available for the hostname. SRV record properties include priority, weight, port, and name (e.g., [{'priority': 10, 'weight': 5, 'port': 21223, 'name': 'service.example.com'}, ...]).
9 dns.resolveSoa(hostname, callback) <br>Similar to dns.resolve(), but only for authoritative records (SOA records).
10 dns.resolveNs(hostname, callback) <br>Similar to dns.resolve(), but only for name server records (NS records). Returns an array of name server records available for the hostname (e.g., ['ns1.example.com', 'ns2.example.com']).
11 dns.resolveCname(hostname, callback) <br>Similar to dns.resolve(), but only for canonical name records (CNAME records). Returns an array of CNAME records available for the hostname (e.g., ['bar.example.com']).
12 dns.reverse(ip, callback) <br>Reverses an IP address to an array of domain names pointing to that IP address.
13 dns.getServers() <br>Returns an array of strings representing the IP addresses used for current DNS resolution.
14 dns.setServers(servers) <br>Specifies an array of IP addresses to be used as the DNS resolution servers.

rrtypes

The following are valid rrtypes for the dns.resolve() method:


Error Codes

Each DNS query may return the following error codes:

Example

Create a main.js file with the following code:

var dns = require('dns');

dns.lookup('www.github.com', function onLookup(err, address, family) {
   console.log('ip address:', address);
   dns.reverse(address, function (err, hostnames) {
   if (err) {
      console.log(err.stack);
   }

   console.log('reverse for ' + address + ': ' + JSON.stringify(hostnames));
});  
});

Executing the above code results in:

address: 192.30.252.130
reverse for 192.30.252.130: ["github.com"]

Node.js Utility Modules

❮ Nodejs Os Module Nodejs Event Loop ❯