Latest web development tutorials

Node.js DNS module

Node.js DNS module

Node.js tool module Node.js tool module

Node.js DNS module is used to resolve domain names.The introduction of DNS module syntax is as follows:

var dns = require("dns")

method

No. Method & description
1 dns.lookup (hostname [, options], callback)
The domain name (eg 'w3big.com') resolved to find first record A (IPV4) or AAAA (IPV6). Parameter options can be an object or an integer. If no options, IP v4 and v6 address can be. If the options are integers, it must be 4 or 6.
2 dns.lookupService (address, port, callback)
Use getnameinfo parsing the incoming address and port for the domain name and services.
3 dns.resolve (hostname [, rrtype], callback)
A domain name (such as 'w3big.com') resolved to an array rrtype specified record type.
4 dns.resolve4 (hostname, callback)
And dns.resolve () is similar, only the query IPv4 (A record). addresses IPv4 address of the array (for example, [ '74.125.79.104', '74 .125.79.105 ', '74 .125.79.106']).
5 dns.resolve6 (hostname, callback)
And dns.resolve4 () is similar, only the query IPv6 (AAAA queries)
6 dns.resolveMx (hostname, callback)
And dns.resolve () is similar, only the query mail exchange (MX record).
7 dns.resolveTxt (hostname, callback)
And dns.resolve () is similar, only for text queries (TXT records). text addresses a 2-d array of records. (For example, [[ 'v = spf1 ip4: 0.0.0.0', '~ all']]). Each sub-array block contains a TXT record. Depending on the situation can be linked together, they can also be used separately.
8 dns.resolveSrv (hostname, callback)
And dns.resolve () is similar, only servicing record query (SRV records). addresses hostname is available SRV record array. SRV record attributes priority (priority), weight (weight), port (port), and the name (name) (for example, [{ 'priority': 10, 'weight': 5, 'port': 21223, 'name ':' service.example.com '}, ...]).
9 dns.resolveSoa (hostname, callback)
And dns.resolve () is similar, only the query Authority record (SOA record).
10 dns.resolveNs (hostname, callback)
And dns.resolve () is similar, only queries the domain name server records (NS records). addresses an array of records is a domain name server (hostname can be used) (for example, [ 'ns1.example.com', 'ns2.example.com']).
11 dns.resolveCname (hostname, callback)
And dns.resolve () is similar, only the records check conducted alias (CNAME record). addresses is an array of records hostname aliases available (eg ,, [ 'bar.example.com']).
12 dns.reverse (ip, callback)
Reverse resolve IP addresses, domain names point to the IP address of the array.
13 dns.getServers ()
Returns a string representing the current resolve the IP address for the array.
14 dns.setServers (servers)
It specifies a set of IP address as the resolution server.

rrtypes

Following is a list dns.resolve () method valid rrtypes values:

  • 'A' the IPV4 address, default
  • 'AAAA' IPV6 address
  • 'MX' mail exchange record
  • 'TXT' text records
  • 'SRV' the SRV records
  • 'PTR' for reverse IP lookup
  • 'NS' domain name server records
  • 'CNAME' alias record
  • 'SOA' initial value of the authorization record

error code

Each DNS query may return the following error code:

  • dns.NODATA : No data response.
  • dns.FORMERR : Query Malformed.
  • dns.SERVFAIL : General Failure.
  • dns.NOTFOUND : Domain not found.
  • dns.NOTIMP : The requested operation is not implemented.
  • dns.REFUSED : refused query.
  • dns.BADQUERY : Query Malformed.
  • dns.BADNAME : domain name is malformed.
  • dns.BADFAMILY : address protocol does not support.
  • dns.BADRESP : Reply malformed.
  • dns.CONNREFUSED : Unable to connect to the DNS server.
  • dns.TIMEOUT : DNS server connection timeout.
  • dns.EOF : end of the file.
  • dns.FILE : Error reading file.
  • dns.NOMEM : memory overflow.
  • dns.DESTRUCTION : channel is destroyed.
  • dns.BADSTR : format string error.
  • dns.BADFLAGS : Illegal identifier.
  • dns.NONAME : given the host is not a number.
  • dns.BADHINTS : Illegal HINTS identifier.
  • dns.NOTINITIALIZED : C C-Ares library was not initialized.
  • dns.LOADIPHLPAPI : Error loading iphlpapi.dll.
  • dns.ADDRGETNETWORKPARAMS : Unable to find GetNetworkParams function.
  • dns.CANCELLED : Cancel DNS query.

Examples

Create main.js file, the code is as follows:

var dns = require('dns');

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

   console.log('反向解析 ' + address + ': ' + JSON.stringify(hostnames));
});  
});

The above code is executed, the results are as follows:

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

Node.js tool module Node.js tool module