Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Node.js DNS


May 10, 2021 Node.js


Table of contents


Dns

稳定性: 3 - 稳定

This section describes the .JS modules in the Node module, which you can access by calling require('dns')

The DNS module contains functions in two different categories:

1) Use the underlying features of the system to complete name parsing, this process does not require network communication, this classification has only dns.lookup 20> dns.lookup

The following example resolves www.google.com

var dns = require('dns');

dns.lookup('www.google.com', function onLookup(err, addresses, family) {
  console.log('addresses:', addresses);
});

2) Connect to DNS server for name resolution, always use the network to query the domain name. T his classification contains dns.lookup T hese functions do not use the same set of profiles as dns.lookup You can use this classification function if you don't want to use the underlying features of the system for name parsing and DNS queries.

In the following example, the 'www.google.com' resolved and the returned IP address is parsed in reverse:

var dns = require('dns');

dns.resolve4('www.google.com', function (err, addresses) {
  if (err) throw err;

  console.log('addresses: ' + JSON.stringify(addresses));

  addresses.forEach(function (a) {
    dns.reverse(a, function (err, hostnames) {
      if (err) {
        throw err;
      }

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

For more details, refer to The Implementations section.

dns.lookup(hostname[, options], callback)

Resolve a domain 'google.com' the first record found, A (IPV4) or AAA (IPV6). T he options can be an object or an integer. I p v4 and v6 addresses are all available if options are not available. If options integers, they must 4 6

options parameter may be an family that hints and hints properties. B oth properties are optional. I f family is provided, it must 4 6 otherwise both IP v4 and v6 addresses are available. I f hints it can be one or more getaddrinfo and if not, no flags are getaddrinfo M ultiple flag bits can be consolidated by or operation. The following example shows how to options

{
  family: 4,
  hints: dns.ADDRCONFIG | dns.V4MAPPED
}

See supported getaddrinfo for more flag bits.

The callback function contains (err, address, family) address parameter represents an IP v4 or v6 address. family parameter is 4 or 6, address the address family (not necessarily the value of the previously incoming lookup).

When an error err Error object and err.code is the error code. Keep in mind that err.code 'ENOENT' only because the domain name does not exist, but also for other reasons, such as the absence of a file descriptor.

dns.lookup not have to be related to the DNS protocol. It uses the characteristics of the operating system to associate names with addresses.

Implementing these things may be easy, but .js for Node and other programs, so take a minute to read The Idea section before using it.

dns.lookupService(address, port, callback)

Use getnameinfo incoming addresses and ports as domain names and services.

The parameters of this callback (err, hostname, service) hostname and service strings (such 'localhost' and 'http'

When an error err Error object and err.code is the error code.

dns.resolve(hostname[, rrtype], callback)

Resolve a domain name, such as google.com, to an array of rrtype specified record types.

The valid rrtypes values are:

  • 'A' (IPV4 address, default)
  • 'AAAA' (IPV6 address)
  • 'MX' (message exchange record)
  • 'TXT' (text record)
  • 'SRV' (SRV record)
  • 'PTR' (for reverse IP lookups)
  • 'NS' (Domain Name Server Record)
  • 'CNAME' (Alias Record)
  • 'SOA' (initial value of authorization record)

The callback (err, addresses) The type of each item in the addresses depends on the record type, as detailed in the corresponding lookup method below.

When an error err Error object and err.code is the error code.

dns.resolve4(hostname, callback)

Similar dns.resolve() only IPv4 (A records) can A addresses IPv4 address array ['74.125.79.104', '74.125.79.105', '74.125.79.106']

dns.resolve6(hostname, callback)

Similar dns.resolve4() only IPv4 (AAAA AAAA

dns.resolveMx(hostname, callback)

Similar dns.resolve() only message exchange MX records) can be queried.

addresses are an array of MX records, each containing priority and exchange properties [{'priority': 10, 'exchange': 'mx.example.com'},...] )。

dns.resolveTxt(hostname, callback)

Similar dns.resolve() only text queries TXT addresses are an array of 2-d text records. ( For example, [ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ] E ach sub-array contains a recorded TXT block. Depending on usage, you can connect together or use it alone.

dns.resolveSrv(hostname, callback)

Similar dns.resolve() only service record queries SRV addresses are an array of SRV records available for hostname T he SRV record properties have priority (priority), weight(weight), port, and name (name) (e.g., [{'priority': 10, 'weight': 5, 'port': 21223, 'name': 'service.example.com'}, ...] )。

dns.resolveSoa(hostname, callback)

Similar dns.resolve() only authoritative records SOA records) can be queried.

addresses objects that contain the following structures:

{
  nsname: 'ns.example.com',
  hostmaster: 'root.example.com',
  serial: 2013101809,
  refresh: 10000,
  retry: 2400,
  expire: 604800,
  minttl: 3600
}

dns.resolveNs(hostname, callback)

Similar dns.resolve() only domain name server record queries NS addresses an array of domain name server records hostname be used) (e.g., ['ns1.example.com', 'ns2.example.com']

dns.resolveCname(hostname, callback)

Similar dns.resolve() only alias record queries CNAME records) can be made. addresses are hostname records that are available to hostname (for ['bar.example.com']

dns.reverse(ip, callback)

Reversely resolve the IP address and return an array of domain names that point to the IP address.

Callback function (err, hostnames)

When an error err Error object and err.code is the error code.

dns.getServers()

Returns a string of arrays for the currently resolved IP address.

dns.setServers(servers)

Specify a set of IP addresses as the resolution server.

If you specify a port to the address, the port is ignored because the underlying library does not support it.

Passing in invalid parameters throws the following error:

Error codes

Each DNS query may return the following error:

  • dns.NODATA The DNS server returns no data response.
  • dns.FORMERR The DNS server claims that the query is in the wrong format.
  • dns.SERVFAIL DNS server returns generally fail.
  • dns.NOTFOUND domain name found.
  • dns.NOTIMP DNS server did not implement the requested operation.
  • dns.REFUSED The DNS server rejects the query.
  • dns.BADQUERY DNS queries are in the wrong format.
  • dns.BADNAME domain name is in the wrong format.
  • dns.BADFAMILY protocols are not supported.
  • dns.BADRESP DNS reply is in the wrong format.
  • dns.CONNREFUSED to connect to DNS server.
  • dns.TIMEOUT The connection DNS server timed out.
  • dns.EOF End of file.
  • dns.FILE Read the file incorrectly.
  • dns.NOMEM Memory overflow.
  • dns.DESTRUCTION The channel is destroyed.
  • dns.BADSTR string is in the wrong format.
  • dns.BADFLAGS Illegal identifier.
  • dns.NONAME The given host is not a number.
  • dns.BADHINTS Illegal HINTS identifier.
  • dns.NOTINITIALIZED The c c-ares library has not yet been initialized.
  • dns.LOADIPHLPAPI was no error loading .dll iphlpapi.
  • dns.ADDRGETNETWORKPARAMS The GetNetworkParams function could not be found.
  • dns.CANCELLED Cancel dns queries.

Supported getaddrinfo flag

The following can be passed to dns.lookup

  • dns.ADDRCONFIG the type of address supported by the current system. For example, if the current system has at least one IPv4 address configured, the IPv4 address is returned.
  • dns.V4MAPPED If the IPv6 family is specified, but the IPv6 address is not found, the IPv6 address mapped by IPv4 will be returned.

Implementation considerations

Although dns.lookup dns.resolve*/dns.reverse corred network names with network addresses, they behave differently. These differences, though subtle, can have a .js Node's program.

dns.lookup

dns.lookup the same system features as most programs. F or example, dns.lookup ping commands resolve a specified name in the same way. M ost POSIX-like systems, dns.lookup can be adjusted by changing the settings of nsswitch.conf(5) resolv.conf(5) Changing these files will affect other applications in the system.

Although JavaScript calls are asynchronous, its implementation is to synchronize the calls to getaddrinfo(3) B ecause the libv thread pool is fixed in size, calling getaddrinfo(3) too long can degrade other operations in the pool, such as file operations. To reduce this risk, you can adjust the libuv thread pool by increasing the value of 'UV_THREADPOOL_SIZE' to more than 4, see the "official libuvdocumentation" (http://docs.libuv.org/en/latest/threadpool.html).

dns.resolve, functions starting with dns.resolve and dns.reverse

The implementation of these functions is dns.lookup I nstead of getaddrinfo(3) they always make network queries. These operations are asynchronous and have nothing to do with the libv thread pool.

Therefore, these operations do not have a negative impact on other threads, dns.lookup

They do not use dns.lookup (for /etc/hosts