May 10, 2021 Node.js
2. dns.lookup(hostname[, options], callback)
3. dns.lookupService(address, port, callback)
4. dns.resolve(hostname[, rrtype], callback)
5. dns.resolve4(hostname, callback)
6. dns.resolve6(hostname, callback)
7. dns.resolveMx(hostname, callback)
8. dns.resolveTxt(hostname, callback)
9. dns.resolveSrv(hostname, callback)
10.. dns.resolveSoa(hostname, callback)
11.. dns.resolveNs(hostname, callback)
12.. dns.resolveCname(hostname, callback)
13.. dns.reverse(ip, callback)
稳定性: 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.
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.
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.
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.
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']
Similar
dns.resolve4()
only IPv4 (AAAA
AAAA
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'},...]
)。
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.
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'}, ...]
)。
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
}
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']
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']
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.
Returns a string of arrays for the currently resolved IP address.
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:
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.
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.
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
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).
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