- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- env 环境变量
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- module/typescript TS 模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- sqlite 轻型数据库
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
Node.js v25.3.0 文档
- Node.js v25.3.0
-
目录
- 域名系统
- 类:
dns.Resolver dns.getServers()dns.lookup(hostname[, options], callback)dns.lookupService(address, port, callback)dns.resolve(hostname[, rrtype], callback)dns.resolve4(hostname[, options], callback)dns.resolve6(hostname[, options], callback)dns.resolveAny(hostname, callback)dns.resolveCname(hostname, callback)dns.resolveCaa(hostname, callback)dns.resolveMx(hostname, callback)dns.resolveNaptr(hostname, callback)dns.resolveNs(hostname, callback)dns.resolvePtr(hostname, callback)dns.resolveSoa(hostname, callback)dns.resolveSrv(hostname, callback)dns.resolveTlsa(hostname, callback)dns.resolveTxt(hostname, callback)dns.reverse(ip, callback)dns.setDefaultResultOrder(order)dns.getDefaultResultOrder()dns.setServers(servers)- DNS promise API
- 类:
dnsPromises.Resolver resolver.cancel()dnsPromises.getServers()dnsPromises.lookup(hostname[, options])dnsPromises.lookupService(address, port)dnsPromises.resolve(hostname[, rrtype])dnsPromises.resolve4(hostname[, options])dnsPromises.resolve6(hostname[, options])dnsPromises.resolveAny(hostname)dnsPromises.resolveCaa(hostname)dnsPromises.resolveCname(hostname)dnsPromises.resolveMx(hostname)dnsPromises.resolveNaptr(hostname)dnsPromises.resolveNs(hostname)dnsPromises.resolvePtr(hostname)dnsPromises.resolveSoa(hostname)dnsPromises.resolveSrv(hostname)dnsPromises.resolveTlsa(hostname)dnsPromises.resolveTxt(hostname)dnsPromises.reverse(ip)dnsPromises.setDefaultResultOrder(order)dnsPromises.getDefaultResultOrder()dnsPromises.setServers(servers)
- 类:
- 错误代码
- 实现的注意事项
- 类:
- 域名系统
-
导航
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- env 环境变量
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- module/typescript TS 模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- sqlite 轻型数据库
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
- 其他版本
域名系统#>
【DNS】
源代码: lib/dns.js
node:dns 模块用于实现名称解析。例如,可以使用它来查询主机名的 IP 地址。
【The node:dns module enables name resolution. For example, use it to look up IP
addresses of host names.】
虽然以 域名系统 (DNS) 命名,但它并不总是使用 DNS 协议进行查询。dns.lookup() 使用操作系统功能来执行名称解析。它可能不需要进行任何网络通信。要像同一系统上的其他应用那样执行名称解析,请使用 dns.lookup()。
【Although named for the Domain Name System (DNS), it does not always use the
DNS protocol for lookups. dns.lookup() uses the operating system
facilities to perform name resolution. It may not need to perform any network
communication. To perform name resolution the way other applications on the same
system do, use dns.lookup().】
import dns from 'node:dns';
dns.lookup('example.org', (err, address, family) => {
console.log('address: %j family: IPv%s', address, family);
});
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6const dns = require('node:dns');
dns.lookup('example.org', (err, address, family) => {
console.log('address: %j family: IPv%s', address, family);
});
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
node:dns 模块中的所有其他函数都会连接到实际的 DNS 服务器以执行名称解析。它们始终使用网络来执行 DNS 查询。这些函数不会使用 dns.lookup() 所使用的同一组配置文件(例如 /etc/hosts)。使用这些函数可以始终执行 DNS 查询,绕过其他名称解析机制。
【All other functions in the node:dns module connect to an actual DNS server to
perform name resolution. They will always use the network to perform DNS
queries. These functions do not use the same set of configuration files used by
dns.lookup() (e.g. /etc/hosts). Use these functions to always perform
DNS queries, bypassing other name-resolution facilities.】
import dns from 'node:dns';
dns.resolve4('archive.org', (err, addresses) => {
if (err) throw err;
console.log(`addresses: ${JSON.stringify(addresses)}`);
addresses.forEach((a) => {
dns.reverse(a, (err, hostnames) => {
if (err) {
throw err;
}
console.log(`reverse for ${a}: ${JSON.stringify(hostnames)}`);
});
});
});const dns = require('node:dns');
dns.resolve4('archive.org', (err, addresses) => {
if (err) throw err;
console.log(`addresses: ${JSON.stringify(addresses)}`);
addresses.forEach((a) => {
dns.reverse(a, (err, hostnames) => {
if (err) {
throw err;
}
console.log(`reverse for ${a}: ${JSON.stringify(hostnames)}`);
});
});
});
请参阅 实现考虑部分 以获取更多信息。
【See the Implementation considerations section for more information.】
类:dns.Resolver#>
【Class: dns.Resolver】
域名系统请求的独立解析器。
【An independent resolver for DNS requests.】
创建一个新的解析器会使用默认的服务器设置。使用 resolver.setServers() 设置解析器使用的服务器不会影响其他解析器:
【Creating a new resolver uses the default server settings. Setting
the servers used for a resolver using
resolver.setServers() does not affect
other resolvers:】
import { Resolver } from 'node:dns';
const resolver = new Resolver();
resolver.setServers(['4.4.4.4']);
// This request will use the server at 4.4.4.4, independent of global settings.
resolver.resolve4('example.org', (err, addresses) => {
// ...
});const { Resolver } = require('node:dns');
const resolver = new Resolver();
resolver.setServers(['4.4.4.4']);
// This request will use the server at 4.4.4.4, independent of global settings.
resolver.resolve4('example.org', (err, addresses) => {
// ...
});
node:dns 模块提供以下方法:
【The following methods from the node:dns module are available:】
resolver.getServers()resolver.resolve()resolver.resolve4()resolver.resolve6()resolver.resolveAny()resolver.resolveCaa()resolver.resolveCname()resolver.resolveMx()resolver.resolveNaptr()resolver.resolveNs()resolver.resolvePtr()resolver.resolveSoa()resolver.resolveSrv()resolver.resolveTlsa()resolver.resolveTxt()resolver.reverse()resolver.setServers()
Resolver([options])#>
创建新的解析器。
【Create a new resolver.】
options<Object>
resolver.cancel()#>
取消此解析器发出的所有未完成的 DNS 查询。相应的回调将以错误代码 ECANCELLED 被调用。
【Cancel all outstanding DNS queries made by this resolver. The corresponding
callbacks will be called with an error with code ECANCELLED.】
resolver.setLocalAddress([ipv4][, ipv6])#>
解析器实例将从指定的 IP 地址发送其请求。这允许程序在多网络接口系统上使用时指定出站接口。
【The resolver instance will send its requests from the specified IP address. This allows programs to specify outbound interfaces when used on multi-homed systems.】
如果未指定 v4 或 v6 地址,将使用默认值,并且操作系统会自动选择本地地址。
【If a v4 or v6 address is not specified, it is set to the default and the operating system will choose a local address automatically.】
解析器在向 IPv4 DNS 服务器发出请求时将使用 v4 本地地址,在向 IPv6 DNS 服务器发出请求时将使用 v6 本地地址。解析请求的 rrtype 对使用的本地地址没有影响。
【The resolver will use the v4 local address when making requests to IPv4 DNS
servers, and the v6 local address when making requests to IPv6 DNS servers.
The rrtype of resolution requests has no impact on the local address used.】
dns.getServers()#>
- 返回值: <string[]>
返回一个 IP 地址字符串数组,格式按照 RFC 5952,目前配置用于 DNS 解析。如果使用了自定义端口,字符串将包含端口部分。
【Returns an array of IP address strings, formatted according to RFC 5952, that are currently configured for DNS resolution. A string will include a port section if a custom port is used.】
[
'8.8.8.8',
'2001:4860:4860::8888',
'8.8.8.8:1053',
'[2001:4860:4860::8888]:1053',
]
dns.lookup(hostname[, options], callback)#>
hostname<string>options<integer> | <Object>family<integer> | <string> 记录族。必须为4、6或0。出于向后兼容的原因,'IPv4'和'IPv6'分别被解释为4和6。值0表示返回 IPv4 或 IPv6 地址。如果值0与{ all: true }(见下文)一起使用,则根据系统的 DNS 解析器返回 IPv4、IPv6 或两者的地址。默认值:0。hints<number> 一个或多个 支持的getaddrinfo标志。可以通过按位OR运算传递多个标志。all<boolean> 当设置为true时,回调会以数组形式返回所有解析的地址。否则,只返回一个地址。默认值:false。order<string> 当为verbatim时,解析的地址将按原始顺序返回,不进行排序。当为ipv4first时,解析的地址将按将 IPv4 地址放在 IPv6 地址之前的顺序排序。当为ipv6first时,解析的地址将按将 IPv6 地址放在 IPv4 地址之前的顺序排序。默认值:verbatim(地址不重新排序)。默认值可以通过dns.setDefaultResultOrder()或--dns-result-order配置。verbatim<boolean> 当为true时,回调函数会按 DNS 解析器返回的顺序接收 IPv4 和 IPv6 地址。当为false时,IPv4 地址会排在 IPv6 地址之前。此选项将被废弃,建议使用order。当两者同时指定时,order优先级更高。新代码应仅使用order。 默认值:true(地址不重新排序)。默认值可以通过dns.setDefaultResultOrder()或--dns-result-order配置。
callback<Function>
将主机名(例如 'nodejs.org')解析为找到的第一个 A(IPv4)或 AAAA(IPv6)记录。所有 option 属性都是可选的。如果 options 是整数,则必须为 4 或 6 —— 如果未提供 options,则在找到时返回 IPv4 或 IPv6 地址,或两者。
【Resolves a host name (e.g. 'nodejs.org') into the first found A (IPv4) or
AAAA (IPv6) record. All option properties are optional. If options is an
integer, then it must be 4 or 6 – if options is not provided, then
either IPv4 or IPv6 addresses, or both, are returned if found.】
当将 all 选项设置为 true 时,callback 的参数将变为 (err, addresses),其中 addresses 是包含 address 和 family 属性的对象数组。
【With the all option set to true, the arguments for callback change to
(err, addresses), with addresses being an array of objects with the
properties address and family.】
出现错误时,err 是一个 Error 对象,其中 err.code 是错误代码。请记住,err.code 不仅在主机名不存在时会被设置为 'ENOTFOUND',而且在查找以其他方式失败时也会如此,例如没有可用的文件描述符。
【On error, err is an Error object, where err.code is the error code.
Keep in mind that err.code will be set to 'ENOTFOUND' not only when
the host name does not exist but also when the lookup fails in other ways
such as no available file descriptors.】
dns.lookup() 不一定与 DNS 协议有关。
其实现使用了操作系统提供的功能,可以将名称与地址相互关联。
这种实现可能会对任何 Node.js 程序的行为产生微妙但重要的影响。
在使用 dns.lookup() 之前,请花一些时间参考 实现考虑部分。
用法示例:
【Example usage:】
import dns from 'node:dns';
const options = {
family: 6,
hints: dns.ADDRCONFIG | dns.V4MAPPED,
};
dns.lookup('example.org', options, (err, address, family) =>
console.log('address: %j family: IPv%s', address, family));
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
// When options.all is true, the result will be an Array.
options.all = true;
dns.lookup('example.org', options, (err, addresses) =>
console.log('addresses: %j', addresses));
// addresses: [{"address":"2606:2800:21f:cb07:6820:80da:af6b:8b2c","family":6}]const dns = require('node:dns');
const options = {
family: 6,
hints: dns.ADDRCONFIG | dns.V4MAPPED,
};
dns.lookup('example.org', options, (err, address, family) =>
console.log('address: %j family: IPv%s', address, family));
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
// When options.all is true, the result will be an Array.
options.all = true;
dns.lookup('example.org', options, (err, addresses) =>
console.log('addresses: %j', addresses));
// addresses: [{"address":"2606:2800:21f:cb07:6820:80da:af6b:8b2c","family":6}]
如果以其 util.promisify()ed 版本调用此方法,并且 all 未设置为 true,它将返回一个 Promise,该 Promise 包含具有 address 和 family 属性的 Object。
【If this method is invoked as its util.promisify()ed version, and all
is not set to true, it returns a Promise for an Object with address and
family properties.】
支持的 getaddrinfo 标志#>
【Supported getaddrinfo flags】
以下标志可以作为提示传递给 dns.lookup()。
【The following flags can be passed as hints to dns.lookup().】
dns.ADDRCONFIG:将返回的地址类型限制为系统上配置的非回环地址类型。例如,只有在当前系统至少配置了一个 IPv4 地址时,才会返回 IPv4 地址。dns.V4MAPPED:如果指定了 IPv6 家族,但未找到 IPv6 地址,则返回 IPv4 映射的 IPv6 地址。在某些操作系统上不支持(例如 FreeBSD 10.1)。dns.ALL:如果指定了dns.V4MAPPED,则返回解析的 IPv6 地址以及映射的 IPv6 IPv4 地址。
dns.lookupService(address, port, callback)#>
address<string>port<number>callback<Function>
使用操作系统的底层 getnameinfo 实现,将给定的 address 和 port 解析为主机名和服务。
【Resolves the given address and port into a host name and service using
the operating system's underlying getnameinfo implementation.】
如果 address 不是有效的 IP 地址,将会抛出 TypeError。port 会被强制转换为数字。如果它不是合法的端口,将会抛出 TypeError。
【If address is not a valid IP address, a TypeError will be thrown.
The port will be coerced to a number. If it is not a legal port, a TypeError
will be thrown.】
在发生错误时,err 是一个 Error 对象,其中 err.code 是错误代码。
【On an error, err is an Error object, where err.code is the error code.】
import dns from 'node:dns';
dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
console.log(hostname, service);
// Prints: localhost ssh
});const dns = require('node:dns');
dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
console.log(hostname, service);
// Prints: localhost ssh
});
如果以其 util.promisify()ed 版本调用此方法,它将返回一个带有 hostname 和 service 属性的 Object 的 Promise。
【If this method is invoked as its util.promisify()ed version, it returns a
Promise for an Object with hostname and service properties.】
dns.resolve(hostname[, rrtype], callback)#>
hostname<string> 要解析的主机名。rrtype<string> 资源记录类型。默认值:'A'。callback<Function>err<Error>records字符串数组 | 对象数组 | 对象
使用 DNS 协议将主机名(例如 'nodejs.org')解析为资源记录数组。callback 函数的参数为 (err, records)。成功时,records 将是一个资源记录数组。单个结果的类型和结构取决于 rrtype:
【Uses the DNS protocol to resolve a host name (e.g. 'nodejs.org') into an array
of the resource records. The callback function has arguments
(err, records). When successful, records will be an array of resource
records. The type and structure of individual results varies based on rrtype:】
rrtype | records contains | Result type | Shorthand method |
|---|---|---|---|
'A' | IPv4 addresses (default) | <string> | dns.resolve4() |
'AAAA' | IPv6 addresses | <string> | dns.resolve6() |
'ANY' | any records | <Object> | dns.resolveAny() |
'CAA' | CA authorization records | <Object> | dns.resolveCaa() |
'CNAME' | canonical name records | <string> | dns.resolveCname() |
'MX' | mail exchange records | <Object> | dns.resolveMx() |
'NAPTR' | name authority pointer records | <Object> | dns.resolveNaptr() |
'NS' | name server records | <string> | dns.resolveNs() |
'PTR' | pointer records | <string> | dns.resolvePtr() |
'SOA' | start of authority records | <Object> | dns.resolveSoa() |
'SRV' | service records | <Object> | dns.resolveSrv() |
'TLSA' | certificate associations | <Object> | dns.resolveTlsa() |
'TXT' | text records | <string[]> | dns.resolveTxt() |
出现错误时,err 是一个 Error 对象,其中 err.code 是 DNS 错误代码 之一。
【On error, err is an Error object, where err.code is one of the
DNS error codes.】
dns.resolve4(hostname[, options], callback)#>
hostname<string> 要解析的主机名。options<Object>ttl<boolean> 获取每条记录的生存时间(TTL)值。当为true时,回调会接收到一个{ address: '1.2.3.4', ttl: 60 }对象的数组,而不是字符串数组,TTL 以秒为单位表示。
callback<Function>err<Error>addresses字符串数组 | 对象数组
使用 DNS 协议解析 hostname 的 IPv4 地址(A 记录)。传递给 callback 函数的 addresses 参数将包含一个 IPv4 地址数组(例如 ['74.125.79.104', '74.125.79.105', '74.125.79.106'])。
【Uses the DNS protocol to resolve a IPv4 addresses (A records) for the
hostname. The addresses argument passed to the callback function
will contain an array of IPv4 addresses (e.g.
['74.125.79.104', '74.125.79.105', '74.125.79.106']).】
dns.resolve6(hostname[, options], callback)#>
hostname<string> 要解析的主机名。options<Object>ttl<boolean> 获取每条记录的生存时间(TTL)值。当设置为true时,回调函数将接收一个{ address: '0:1:2:3:4:5:6:7', ttl: 60 }对象的数组,而不是字符串数组,TTL 以秒为单位表示。
callback<Function>err<Error>addresses字符串数组 | 对象数组
使用 DNS 协议解析 hostname 的 IPv6 地址(AAAA 记录)。传递给 callback 函数的 addresses 参数将包含一个 IPv6 地址数组。
【Uses the DNS protocol to resolve IPv6 addresses (AAAA records) for the
hostname. The addresses argument passed to the callback function
will contain an array of IPv6 addresses.】
dns.resolveAny(hostname, callback)#>
hostname<string>callback<Function>err<Error>ret对象数组[]
使用 DNS 协议解析所有记录(也称为 ANY 或 * 查询)。
传递给 callback 函数的 ret 参数将是一个包含各种类型记录的数组。每个对象都有一个 type 属性,用于指示当前记录的类型。根据 type 的不同,对象上还会存在其他属性:
【Uses the DNS protocol to resolve all records (also known as ANY or * query).
The ret argument passed to the callback function will be an array containing
various types of records. Each object has a property type that indicates the
type of the current record. And depending on the type, additional properties
will be present on the object:】
| Type | Properties |
|---|---|
'A' | address/ttl |
'AAAA' | address/ttl |
'CNAME' | value |
'MX' | Refer to dns.resolveMx() |
'NAPTR' | Refer to dns.resolveNaptr() |
'NS' | value |
'PTR' | value |
'SOA' | Refer to dns.resolveSoa() |
'SRV' | Refer to dns.resolveSrv() |
'TLSA' | Refer to dns.resolveTlsa() |
'TXT' | This type of record contains an array property called entries which refers to dns.resolveTxt(), e.g. { entries: ['...'], type: 'TXT' } |
以下是传递给回调函数的 ret 对象示例:
【Here is an example of the ret object passed to the callback:】
[ { type: 'A', address: '127.0.0.1', ttl: 299 },
{ type: 'CNAME', value: 'example.com' },
{ type: 'MX', exchange: 'alt4.aspmx.l.example.com', priority: 50 },
{ type: 'NS', value: 'ns1.example.com' },
{ type: 'TXT', entries: [ 'v=spf1 include:_spf.example.com ~all' ] },
{ type: 'SOA',
nsname: 'ns1.example.com',
hostmaster: 'admin.example.com',
serial: 156696742,
refresh: 900,
retry: 900,
expire: 1800,
minttl: 60 } ]
DNS 服务器运营商可能选择不响应 ANY 查询。最好调用单独的方法,例如 dns.resolve4()、dns.resolveMx() 等。更多详情,请参见 RFC 8482。
【DNS server operators may choose not to respond to ANY
queries. It may be better to call individual methods like dns.resolve4(),
dns.resolveMx(), and so on. For more details, see RFC 8482.】
dns.resolveCname(hostname, callback)#>
hostname<string>callback<Function>err<Error>addresses字符串数组
使用 DNS 协议解析 hostname 的 CNAME 记录。传递给 callback 函数的 addresses 参数将包含 hostname 可用的规范名称记录数组(例如 ['bar.example.com'])。
【Uses the DNS protocol to resolve CNAME records for the hostname. The
addresses argument passed to the callback function
will contain an array of canonical name records available for the hostname
(e.g. ['bar.example.com']).】
dns.resolveCaa(hostname, callback)#>
hostname<string>callback<Function>err<Error>records对象数组
使用 DNS 协议解析 hostname 的 CAA 记录。传递给 callback 函数的 addresses 参数将包含 hostname 可用的证书颁发机构授权记录数组(例如 [{critical: 0, iodef: 'mailto:pki@example.com'}, {critical: 128, issue: 'pki.example.com'}])。
【Uses the DNS protocol to resolve CAA records for the hostname. The
addresses argument passed to the callback function
will contain an array of certification authority authorization records
available for the hostname (e.g. [{critical: 0, iodef: 'mailto:pki@example.com'}, {critical: 128, issue: 'pki.example.com'}]).】
dns.resolveMx(hostname, callback)#>
hostname<string>callback<Function>err<Error>addresses对象数组
使用 DNS 协议解析 hostname 的邮件交换记录(MX 记录)。传递给 callback 函数的 addresses 参数将包含一个对象数组,每个对象包含 priority 和 exchange 属性(例如 [{priority: 10, exchange: 'mx.example.com'}, ...])。
【Uses the DNS protocol to resolve mail exchange records (MX records) for the
hostname. The addresses argument passed to the callback function will
contain an array of objects containing both a priority and exchange
property (e.g. [{priority: 10, exchange: 'mx.example.com'}, ...]).】
dns.resolveNaptr(hostname, callback)#>
hostname<string>callback<Function>err<Error>addresses对象数组
使用 DNS 协议解析基于正则表达式的记录(NAPTR 记录)以获取 hostname。传递给 callback 函数的 addresses 参数将包含一个具有以下属性的对象数组:
【Uses the DNS protocol to resolve regular expression-based records (NAPTR
records) for the hostname. The addresses argument passed to the callback
function will contain an array of objects with the following properties:】
flagsserviceregexpreplacementorderpreference
{
flags: 's',
service: 'SIP+D2U',
regexp: '',
replacement: '_sip._udp.example.com',
order: 30,
preference: 100
}
dns.resolveNs(hostname, callback)#>
hostname<string>callback<Function>err<Error>addresses字符串数组
使用 DNS 协议解析 hostname 的名称服务器记录(NS 记录)。传递给 callback 函数的 addresses 参数将包含 hostname 可用的名称服务器记录数组(例如:['ns1.example.com', 'ns2.example.com'])。
【Uses the DNS protocol to resolve name server records (NS records) for the
hostname. The addresses argument passed to the callback function will
contain an array of name server records available for hostname
(e.g. ['ns1.example.com', 'ns2.example.com']).】
dns.resolvePtr(hostname, callback)#>
hostname<string>callback<Function>err<Error>addresses字符串数组
使用 DNS 协议来解析主机名(hostname)的指针记录(PTR 记录)。传递给 callback 函数的 addresses 参数将是一个包含回复记录的字符串数组。
【Uses the DNS protocol to resolve pointer records (PTR records) for the
hostname. The addresses argument passed to the callback function will
be an array of strings containing the reply records.】
dns.resolveSoa(hostname, callback)#>
hostname<string>callback<Function>
使用 DNS 协议解析 hostname 的授权起始记录(SOA 记录)。传递给 callback 函数的 address 参数将是一个具有以下属性的对象:
【Uses the DNS protocol to resolve a start of authority record (SOA record) for
the hostname. The address argument passed to the callback function will
be an object with the following properties:】
nsnamehostmasterserialrefreshretryexpireminttl
{
nsname: 'ns.example.com',
hostmaster: 'root.example.com',
serial: 2013101809,
refresh: 10000,
retry: 2400,
expire: 604800,
minttl: 3600
}
dns.resolveSrv(hostname, callback)#>
hostname<string>callback<Function>err<Error>addresses对象数组
使用 DNS 协议解析 hostname 的服务记录(SRV 记录)。传递给 callback 函数的 addresses 参数将是一个包含以下属性的对象数组:
【Uses the DNS protocol to resolve service records (SRV records) for the
hostname. The addresses argument passed to the callback function will
be an array of objects with the following properties:】
priorityweightportname
{
priority: 10,
weight: 5,
port: 21223,
name: 'service.example.com'
}
dns.resolveTlsa(hostname, callback)#>
hostname<string>callback<Function>err<Error>records对象数组
使用 DNS 协议解析证书关联(TLSA 记录)以获取 hostname。传递给 callback 函数的 records 参数是一个包含以下属性的对象数组:
【Uses the DNS protocol to resolve certificate associations (TLSA records) for
the hostname. The records argument passed to the callback function is an
array of objects with these properties:】
certUsageselectormatchdata
{
certUsage: 3,
selector: 1,
match: 1,
data: [ArrayBuffer]
}
dns.resolveTxt(hostname, callback)#>
hostname<string>callback<Function>err<Error>records字符串数组
使用 DNS 协议解析针对 hostname 的文本查询(TXT 记录)。传递给 callback 函数的 records 参数是一个二维数组,包含 hostname 可用的文本记录(例如 [ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ])。每个子数组包含一条记录的 TXT 块。根据使用情况,这些块可以被合并在一起,也可以单独处理。
【Uses the DNS protocol to resolve text queries (TXT records) for the
hostname. The records argument passed to the callback function is a
two-dimensional array of the text records available for hostname (e.g.
[ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ]). Each sub-array contains TXT chunks of
one record. Depending on the use case, these could be either joined together or
treated separately.】
dns.reverse(ip, callback)#>
ip<string>callback<Function>err<Error>hostnames字符串数组
执行反向 DNS 查询,将 IPv4 或 IPv6 地址解析为主机名数组。
【Performs a reverse DNS query that resolves an IPv4 or IPv6 address to an array of host names.】
出现错误时,err 是一个 Error 对象,其中 err.code 是 DNS 错误代码 之一。
【On error, err is an Error object, where err.code is
one of the DNS error codes.】
dns.setDefaultResultOrder(order)#>
order<string> 必须是'ipv4first'、'ipv6first'或'verbatim'。
在 dns.lookup() 和 dnsPromises.lookup() 中设置 order 的默认值。该值可以是:
【Set the default value of order in dns.lookup() and
dnsPromises.lookup(). The value could be:】
ipv4first:将默认order设置为ipv4first。ipv6first:将默认order设置为ipv6first。verbatim:将默认order设置为verbatim。
默认值是 verbatim,dns.setDefaultResultOrder() 的优先级高于 --dns-result-order。使用 工作线程 时,主线程的 dns.setDefaultResultOrder() 不会影响工作线程中默认的 DNS 顺序。
【The default is verbatim and dns.setDefaultResultOrder() have higher
priority than --dns-result-order. When using worker threads,
dns.setDefaultResultOrder() from the main thread won't affect the default
dns orders in workers.】
dns.getDefaultResultOrder()#>
获取 dns.lookup() 和 dnsPromises.lookup() 中 order 的默认值。该值可能是:
【Get the default value for order in dns.lookup() and
dnsPromises.lookup(). The value could be:】
ipv4first:用于order,默认值为ipv4first。ipv6first:用于order,默认值为ipv6first。verbatim:用于将order默认为verbatim。
dns.setServers(servers)#>
servers<string[]> RFC 5952 格式地址的数组
设置在执行 DNS 解析时要使用的服务器的 IP 地址和端口。servers 参数是一个 RFC 5952 格式地址的数组。如果端口是 IANA 默认的 DNS 端口(53),可以省略不写。
【Sets the IP address and port of servers to be used when performing DNS
resolution. The servers argument is an array of RFC 5952 formatted
addresses. If the port is the IANA default DNS port (53) it can be omitted.】
dns.setServers([
'8.8.8.8',
'[2001:4860:4860::8888]',
'8.8.8.8:1053',
'[2001:4860:4860::8888]:1053',
]);
如果提供的地址无效,则会抛出错误。
【An error will be thrown if an invalid address is provided.】
dns.setServers() 方法在 DNS 查询进行时不得被调用。
【The dns.setServers() method must not be called while a DNS query is in
progress.】
dns.setServers() 方法只影响 dns.resolve()、dns.resolve*() 和 dns.reverse()(尤其不影响 dns.lookup())。
【The dns.setServers() method affects only dns.resolve(),
dns.resolve*() and dns.reverse() (and specifically not
dns.lookup()).】
这种方法的工作方式与resolve.conf非常相似。也就是说,如果尝试使用提供的第一个服务器解析时出现NOTFOUND错误,resolve()方法将不会尝试使用随后提供的服务器进行解析。只有在前面的服务器超时或出现其他错误时,才会使用备用DNS服务器。
【This method works much like
resolve.conf.
That is, if attempting to resolve with the first server provided results in a
NOTFOUND error, the resolve() method will not attempt to resolve with
subsequent servers provided. Fallback DNS servers will only be used if the
earlier ones time out or result in some other error.】
DNS promise API#>
【DNS promises API】
dns.promises API 提供了一组替代的异步 DNS 方法,这些方法返回 Promise 对象,而不是使用回调函数。该 API 可以通过 require('node:dns').promises 或 require('node:dns/promises') 访问。
【The dns.promises API provides an alternative set of asynchronous DNS methods
that return Promise objects rather than using callbacks. The API is accessible
via require('node:dns').promises or require('node:dns/promises').】
类:dnsPromises.Resolver#>
【Class: dnsPromises.Resolver】
域名系统请求的独立解析器。
【An independent resolver for DNS requests.】
创建一个新的解析器会使用默认的服务器设置。使用 resolver.setServers() 设置解析器使用的服务器不会影响其他解析器:
【Creating a new resolver uses the default server settings. Setting
the servers used for a resolver using
resolver.setServers() does not affect
other resolvers:】
import { Resolver } from 'node:dns/promises';
const resolver = new Resolver();
resolver.setServers(['4.4.4.4']);
// This request will use the server at 4.4.4.4, independent of global settings.
const addresses = await resolver.resolve4('example.org');const { Resolver } = require('node:dns').promises;
const resolver = new Resolver();
resolver.setServers(['4.4.4.4']);
// This request will use the server at 4.4.4.4, independent of global settings.
resolver.resolve4('example.org').then((addresses) => {
// ...
});
// Alternatively, the same code can be written using async-await style.
(async function() {
const addresses = await resolver.resolve4('example.org');
})();
dnsPromises API 提供以下方法:
【The following methods from the dnsPromises API are available:】
resolver.getServers()resolver.resolve()resolver.resolve4()resolver.resolve6()resolver.resolveAny()resolver.resolveCaa()resolver.resolveCname()resolver.resolveMx()resolver.resolveNaptr()resolver.resolveNs()resolver.resolvePtr()resolver.resolveSoa()resolver.resolveSrv()resolver.resolveTlsa()resolver.resolveTxt()resolver.reverse()resolver.setServers()
resolver.cancel()#>
取消此解析器发出的所有未完成 DNS 查询。相应的承诺将被拒绝,并返回代码为 ECANCELLED 的错误。
【Cancel all outstanding DNS queries made by this resolver. The corresponding
promises will be rejected with an error with the code ECANCELLED.】
dnsPromises.getServers()#>
- 返回值: <string[]>
返回一个 IP 地址字符串数组,格式按照 RFC 5952,目前配置用于 DNS 解析。如果使用了自定义端口,字符串将包含端口部分。
【Returns an array of IP address strings, formatted according to RFC 5952, that are currently configured for DNS resolution. A string will include a port section if a custom port is used.】
[
'8.8.8.8',
'2001:4860:4860::8888',
'8.8.8.8:1053',
'[2001:4860:4860::8888]:1053',
]
dnsPromises.lookup(hostname[, options])#>
hostname<string>options<integer> | <Object>family<integer> 记录的类型。必须是4、6或0。值0表示返回的是 IPv4 或 IPv6 地址。如果在{ all: true }(见下文)中使用值0,则根据系统的 DNS 解析器,可能返回其中一种或两种地址。默认值:0。hints<number> 一个或多个 支持的getaddrinfo标志。可以通过按位OR运算传递多个标志。all<boolean> 当为true时,Promise会返回包含所有地址的数组。否则,只返回一个地址。默认值:false。order<string> 当为verbatim时,Promise会按 DNS 解析器返回的顺序解析 IPv4 和 IPv6 地址。 当为ipv4first时,IPv4 地址会排在 IPv6 地址之前。 当为ipv6first时,IPv6 地址会排在 IPv4 地址之前。 默认值:verbatim(地址不会重新排序)。 默认值可以通过dns.setDefaultResultOrder()或--dns-result-order配置。 新代码应使用{ order: 'verbatim' }。verbatim<boolean> 当true时,Promise会按 DNS 解析器返回的顺序解析 IPv4 和 IPv6 地址。当false时,IPv4 地址会放在 IPv6 地址之前。此选项将被弃用,取而代之的是order。当两者都指定时,order优先。新代码应仅使用order。默认值: 目前为false(地址会被重新排序),但预计在不久的将来会改变。默认值可使用dns.setDefaultResultOrder()或--dns-result-order配置。
将主机名(例如 'nodejs.org')解析为找到的第一个 A(IPv4)或 AAAA(IPv6)记录。所有 option 属性都是可选的。如果 options 是整数,则必须为 4 或 6 —— 如果未提供 options,则在找到时返回 IPv4 或 IPv6 地址,或两者。
【Resolves a host name (e.g. 'nodejs.org') into the first found A (IPv4) or
AAAA (IPv6) record. All option properties are optional. If options is an
integer, then it must be 4 or 6 – if options is not provided, then
either IPv4 or IPv6 addresses, or both, are returned if found.】
当 all 选项设置为 true 时,Promise 会被解析为 addresses,它是一个对象数组,每个对象包含 address 和 family 属性。
【With the all option set to true, the Promise is resolved with addresses
being an array of objects with the properties address and family.】
在出错时,Promise 会被 Error 对象拒绝,其中 err.code 是错误代码。请记住,err.code 不仅在主机名不存在时会被设置为 'ENOTFOUND',在其他查找失败的情况下(例如没有可用的文件描述符)也会如此。
【On error, the Promise is rejected with an Error object, where err.code
is the error code.
Keep in mind that err.code will be set to 'ENOTFOUND' not only when
the host name does not exist but also when the lookup fails in other ways
such as no available file descriptors.】
dnsPromises.lookup() 不一定与 DNS 协议有任何关系。该实现使用操作系统的功能,可以将名称与地址相互关联。此实现可能对任何 Node.js 程序的行为产生微妙但重要的影响。在使用 dnsPromises.lookup() 之前,请花一些时间查阅 实现考虑部分。
用法示例:
【Example usage:】
import dns from 'node:dns';
const dnsPromises = dns.promises;
const options = {
family: 6,
hints: dns.ADDRCONFIG | dns.V4MAPPED,
};
await dnsPromises.lookup('example.org', options).then((result) => {
console.log('address: %j family: IPv%s', result.address, result.family);
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
});
// When options.all is true, the result will be an Array.
options.all = true;
await dnsPromises.lookup('example.org', options).then((result) => {
console.log('addresses: %j', result);
// addresses: [{"address":"2606:2800:21f:cb07:6820:80da:af6b:8b2c","family":6}]
});const dns = require('node:dns');
const dnsPromises = dns.promises;
const options = {
family: 6,
hints: dns.ADDRCONFIG | dns.V4MAPPED,
};
dnsPromises.lookup('example.org', options).then((result) => {
console.log('address: %j family: IPv%s', result.address, result.family);
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
});
// When options.all is true, the result will be an Array.
options.all = true;
dnsPromises.lookup('example.org', options).then((result) => {
console.log('addresses: %j', result);
// addresses: [{"address":"2606:2800:21f:cb07:6820:80da:af6b:8b2c","family":6}]
});
dnsPromises.lookupService(address, port)#>
使用操作系统的底层 getnameinfo 实现,将给定的 address 和 port 解析为主机名和服务。
【Resolves the given address and port into a host name and service using
the operating system's underlying getnameinfo implementation.】
如果 address 不是有效的 IP 地址,将会抛出 TypeError。port 会被强制转换为数字。如果它不是合法的端口,将会抛出 TypeError。
【If address is not a valid IP address, a TypeError will be thrown.
The port will be coerced to a number. If it is not a legal port, a TypeError
will be thrown.】
出现错误时,Promise 会被 Error 对象拒绝,其中 err.code 是错误代码。
【On error, the Promise is rejected with an Error object, where err.code
is the error code.】
import dnsPromises from 'node:dns/promises';
const result = await dnsPromises.lookupService('127.0.0.1', 22);
console.log(result.hostname, result.service); // Prints: localhost sshconst dnsPromises = require('node:dns').promises;
dnsPromises.lookupService('127.0.0.1', 22).then((result) => {
console.log(result.hostname, result.service);
// Prints: localhost ssh
});
dnsPromises.resolve(hostname[, rrtype])#>
使用 DNS 协议将主机名(例如 'nodejs.org')解析为资源记录数组。成功时,Promise 会解析为一个资源记录数组。单个结果的类型和结构取决于 rrtype:
【Uses the DNS protocol to resolve a host name (e.g. 'nodejs.org') into an array
of the resource records. When successful, the Promise is resolved with an
array of resource records. The type and structure of individual results vary
based on rrtype:】
rrtype | records contains | Result type | Shorthand method |
|---|---|---|---|
'A' | IPv4 addresses (default) | <string> | dnsPromises.resolve4() |
'AAAA' | IPv6 addresses | <string> | dnsPromises.resolve6() |
'ANY' | any records | <Object> | dnsPromises.resolveAny() |
'CAA' | CA authorization records | <Object> | dnsPromises.resolveCaa() |
'CNAME' | canonical name records | <string> | dnsPromises.resolveCname() |
'MX' | mail exchange records | <Object> | dnsPromises.resolveMx() |
'NAPTR' | name authority pointer records | <Object> | dnsPromises.resolveNaptr() |
'NS' | name server records | <string> | dnsPromises.resolveNs() |
'PTR' | pointer records | <string> | dnsPromises.resolvePtr() |
'SOA' | start of authority records | <Object> | dnsPromises.resolveSoa() |
'SRV' | service records | <Object> | dnsPromises.resolveSrv() |
'TLSA' | certificate associations | <Object> | dnsPromises.resolveTlsa() |
'TXT' | text records | <string[]> | dnsPromises.resolveTxt() |
在发生错误时,Promise 会被 Error 对象拒绝,其中 err.code 是 DNS 错误代码 之一。
【On error, the Promise is rejected with an Error object, where err.code
is one of the DNS error codes.】
dnsPromises.resolve4(hostname[, options])#>
hostname<string> 要解析的主机名。options<Object>ttl<boolean> 获取每条记录的生存时间(TTL)值。当设置为true时,Promise将解析为一个包含{ address: '1.2.3.4', ttl: 60 }对象的数组,而不是字符串数组,TTL 以秒为单位表示。
使用 DNS 协议解析 hostname 的 IPv4 地址(A 记录)。成功时,Promise 会被解析为一个 IPv4 地址数组(例如 ['74.125.79.104', '74.125.79.105', '74.125.79.106'])。
【Uses the DNS protocol to resolve IPv4 addresses (A records) for the
hostname. On success, the Promise is resolved with an array of IPv4
addresses (e.g. ['74.125.79.104', '74.125.79.105', '74.125.79.106']).】
dnsPromises.resolve6(hostname[, options])#>
hostname<string> 要解析的主机名。options<Object>ttl<boolean> 获取每条记录的生存时间(TTL)值。当设置为true时,Promise会解析为一个包含{ address: '0:1:2:3:4:5:6:7', ttl: 60 }对象的数组,而不是字符串数组,TTL 值以秒为单位表示。
使用 DNS 协议解析 hostname 的 IPv6 地址(AAAA 记录)。成功时,Promise 会被解析为一个包含 IPv6 地址的数组。
【Uses the DNS protocol to resolve IPv6 addresses (AAAA records) for the
hostname. On success, the Promise is resolved with an array of IPv6
addresses.】
dnsPromises.resolveAny(hostname)#>
hostname<string>
使用 DNS 协议解析所有记录(也称为 ANY 或 * 查询)。
成功时,Promise 会以一个包含各种类型记录的数组形式被解析。每个对象都有一个 type 属性,用于指示当前记录的类型。根据 type,对象上还会有其他属性:
【Uses the DNS protocol to resolve all records (also known as ANY or * query).
On success, the Promise is resolved with an array containing various types of
records. Each object has a property type that indicates the type of the
current record. And depending on the type, additional properties will be
present on the object:】
| Type | Properties |
|---|---|
'A' | address/ttl |
'AAAA' | address/ttl |
'CNAME' | value |
'MX' | Refer to dnsPromises.resolveMx() |
'NAPTR' | Refer to dnsPromises.resolveNaptr() |
'NS' | value |
'PTR' | value |
'SOA' | Refer to dnsPromises.resolveSoa() |
'SRV' | Refer to dnsPromises.resolveSrv() |
'TLSA' | Refer to dnsPromises.resolveTlsa() |
'TXT' | This type of record contains an array property called entries which refers to dnsPromises.resolveTxt(), e.g. { entries: ['...'], type: 'TXT' } |
以下是结果对象的示例:
【Here is an example of the result object:】
[ { type: 'A', address: '127.0.0.1', ttl: 299 },
{ type: 'CNAME', value: 'example.com' },
{ type: 'MX', exchange: 'alt4.aspmx.l.example.com', priority: 50 },
{ type: 'NS', value: 'ns1.example.com' },
{ type: 'TXT', entries: [ 'v=spf1 include:_spf.example.com ~all' ] },
{ type: 'SOA',
nsname: 'ns1.example.com',
hostmaster: 'admin.example.com',
serial: 156696742,
refresh: 900,
retry: 900,
expire: 1800,
minttl: 60 } ]
dnsPromises.resolveCaa(hostname)#>
hostname<string>
使用 DNS 协议解析 hostname 的 CAA 记录。成功时,Promise 会解析为包含可用于 hostname 的认证机构授权记录的对象数组(例如 [{critical: 0, iodef: 'mailto:pki@example.com'},{critical: 128, issue: 'pki.example.com'}])。
【Uses the DNS protocol to resolve CAA records for the hostname. On success,
the Promise is resolved with an array of objects containing available
certification authority authorization records available for the hostname
(e.g. [{critical: 0, iodef: 'mailto:pki@example.com'},{critical: 128, issue: 'pki.example.com'}]).】
dnsPromises.resolveCname(hostname)#>
hostname<string>
使用 DNS 协议解析 hostname 的 CNAME 记录。成功时,Promise 会被解析为包含 hostname 可用的规范名称记录的数组(例如 ['bar.example.com'])。
【Uses the DNS protocol to resolve CNAME records for the hostname. On success,
the Promise is resolved with an array of canonical name records available for
the hostname (e.g. ['bar.example.com']).】
dnsPromises.resolveMx(hostname)#>
hostname<string>
使用 DNS 协议解析 hostname 的邮件交换记录(MX 记录)。成功时,Promise 会被解析为包含对象数组,每个对象包含 priority 和 exchange 属性(例如 [{priority: 10, exchange: 'mx.example.com'}, ...])。
【Uses the DNS protocol to resolve mail exchange records (MX records) for the
hostname. On success, the Promise is resolved with an array of objects
containing both a priority and exchange property (e.g.
[{priority: 10, exchange: 'mx.example.com'}, ...]).】
dnsPromises.resolveNaptr(hostname)#>
hostname<string>
使用 DNS 协议解析基于正则表达式的记录(NAPTR 记录)以获取 hostname。成功时,Promise 会被解析为一个包含以下属性的对象数组:
【Uses the DNS protocol to resolve regular expression-based records (NAPTR
records) for the hostname. On success, the Promise is resolved with an array
of objects with the following properties:】
flagsserviceregexpreplacementorderpreference
{
flags: 's',
service: 'SIP+D2U',
regexp: '',
replacement: '_sip._udp.example.com',
order: 30,
preference: 100
}
dnsPromises.resolveNs(hostname)#>
hostname<string>
使用 DNS 协议解析 hostname 的名称服务器记录(NS 记录)。成功时,Promise 会被解析为一个包含 hostname 可用名称服务器记录的数组(例如 ['ns1.example.com', 'ns2.example.com'])。
【Uses the DNS protocol to resolve name server records (NS records) for the
hostname. On success, the Promise is resolved with an array of name server
records available for hostname (e.g.
['ns1.example.com', 'ns2.example.com']).】
dnsPromises.resolvePtr(hostname)#>
hostname<string>
使用 DNS 协议解析 hostname 的指针记录(PTR 记录)。成功时,Promise 会被解析为包含响应记录的字符串数组。
【Uses the DNS protocol to resolve pointer records (PTR records) for the
hostname. On success, the Promise is resolved with an array of strings
containing the reply records.】
dnsPromises.resolveSoa(hostname)#>
hostname<string>
使用 DNS 协议解析 hostname 的权限起始记录(SOA 记录)。成功时,Promise 将以一个具有以下属性的对象形式被解析:
【Uses the DNS protocol to resolve a start of authority record (SOA record) for
the hostname. On success, the Promise is resolved with an object with the
following properties:】
nsnamehostmasterserialrefreshretryexpireminttl
{
nsname: 'ns.example.com',
hostmaster: 'root.example.com',
serial: 2013101809,
refresh: 10000,
retry: 2400,
expire: 604800,
minttl: 3600
}
dnsPromises.resolveSrv(hostname)#>
hostname<string>
使用 DNS 协议解析 hostname 的服务记录(SRV 记录)。成功时,Promise 会解析为包含以下属性的对象数组:
【Uses the DNS protocol to resolve service records (SRV records) for the
hostname. On success, the Promise is resolved with an array of objects with
the following properties:】
priorityweightportname
{
priority: 10,
weight: 5,
port: 21223,
name: 'service.example.com'
}
dnsPromises.resolveTlsa(hostname)#>
hostname<string>
使用 DNS 协议解析 hostname 的证书关联(TLSA 记录)。成功时,Promise 会解析为包含具有以下属性的对象数组:
【Uses the DNS protocol to resolve certificate associations (TLSA records) for
the hostname. On success, the Promise is resolved with an array of objects
with these properties:】
certUsageselectormatchdata
{
certUsage: 3,
selector: 1,
match: 1,
data: [ArrayBuffer]
}
dnsPromises.resolveTxt(hostname)#>
hostname<string>
使用 DNS 协议解析 hostname 的文本查询(TXT 记录)。成功时,Promise 会被解析为一个二维数组,包含 hostname 可用的文本记录(例如 [ ['v=spf1 ip4:0.0.0.0 ', '~all'] ])。每个子数组包含一条记录的 TXT 块。根据使用情况,这些块可以合并在一起或单独处理。
【Uses the DNS protocol to resolve text queries (TXT records) for the
hostname. On success, the Promise is resolved with a two-dimensional array
of the text records available for hostname (e.g.
[ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ]). Each sub-array contains TXT chunks of
one record. Depending on the use case, these could be either joined together or
treated separately.】
dnsPromises.reverse(ip)#>
ip<string>
执行反向 DNS 查询,将 IPv4 或 IPv6 地址解析为主机名数组。
【Performs a reverse DNS query that resolves an IPv4 or IPv6 address to an array of host names.】
在发生错误时,Promise 会被 Error 对象拒绝,其中 err.code 是 DNS 错误代码 之一。
【On error, the Promise is rejected with an Error object, where err.code
is one of the DNS error codes.】
dnsPromises.setDefaultResultOrder(order)#>
order<string> 必须是'ipv4first'、'ipv6first'或'verbatim'。
在 dns.lookup() 和 dnsPromises.lookup() 中设置 order 的默认值。该值可以是:
【Set the default value of order in dns.lookup() and
dnsPromises.lookup(). The value could be:】
ipv4first:将默认order设置为ipv4first。ipv6first:将默认order设置为ipv6first。verbatim:将默认order设置为verbatim。
默认值是 verbatim,dnsPromises.setDefaultResultOrder() 的优先级高于 --dns-result-order。使用 工作线程 时,主线程的 dnsPromises.setDefaultResultOrder() 不会影响工作线程中的默认 DNS 顺序。
【The default is verbatim and dnsPromises.setDefaultResultOrder() have
higher priority than --dns-result-order. When using worker threads,
dnsPromises.setDefaultResultOrder() from the main thread won't affect the
default dns orders in workers.】
dnsPromises.getDefaultResultOrder()#>
获取 dnsOrder 的值。
【Get the value of dnsOrder.】
dnsPromises.setServers(servers)#>
servers<string[]> RFC 5952 格式地址的数组
设置在执行 DNS 解析时要使用的服务器的 IP 地址和端口。servers 参数是一个 RFC 5952 格式地址的数组。如果端口是 IANA 默认的 DNS 端口(53),可以省略不写。
【Sets the IP address and port of servers to be used when performing DNS
resolution. The servers argument is an array of RFC 5952 formatted
addresses. If the port is the IANA default DNS port (53) it can be omitted.】
dnsPromises.setServers([
'8.8.8.8',
'[2001:4860:4860::8888]',
'8.8.8.8:1053',
'[2001:4860:4860::8888]:1053',
]);
如果提供的地址无效,则会抛出错误。
【An error will be thrown if an invalid address is provided.】
dnsPromises.setServers() 方法在 DNS 查询进行时不得调用。
【The dnsPromises.setServers() method must not be called while a DNS query is in
progress.】
这种方法的工作方式与resolve.conf非常相似。也就是说,如果尝试使用提供的第一个服务器解析时出现NOTFOUND错误,resolve()方法将不会尝试使用随后提供的服务器进行解析。只有在前面的服务器超时或出现其他错误时,才会使用备用DNS服务器。
【This method works much like
resolve.conf.
That is, if attempting to resolve with the first server provided results in a
NOTFOUND error, the resolve() method will not attempt to resolve with
subsequent servers provided. Fallback DNS servers will only be used if the
earlier ones time out or result in some other error.】
错误代码#>
【Error codes】
每个域名系统查询都可以返回以下错误代码之一:
【Each DNS query can return one of the following error codes:】
dns.NODATA:DNS 服务器返回了一个没有数据的响应。dns.FORMERR:DNS 服务器表示查询格式错误。dns.SERVFAIL:DNS 服务器返回一般性故障。dns.NOTFOUND:未找到域名。dns.NOTIMP:DNS 服务器未实现请求的操作。dns.REFUSED:DNS 服务器拒绝查询。dns.BADQUERY:格式错误的 DNS 查询。dns.BADNAME:格式错误的主机名。dns.BADFAMILY:不支持的地址族。dns.BADRESP:格式错误的 DNS 响应。dns.CONNREFUSED:无法联系 DNS 服务器。dns.TIMEOUT:联系 DNS 服务器时超时。dns.EOF:文件结束。dns.FILE:读取文件时出错。dns.NOMEM: 内存不足。dns.DESTRUCTION:通道正在被销毁。dns.BADSTR:格式错误的字符串。dns.BADFLAGS:指定了非法标志。dns.NONAME: 给定的主机名不是数字格式。dns.BADHINTS:指定了非法的提示标志。dns.NOTINITIALIZED:c-ares 库尚未初始化。dns.LOADIPHLPAPI:加载iphlpapi.dll时出错。dns.ADDRGETNETWORKPARAMS:找不到GetNetworkParams函数。dns.CANCELLED:DNS 查询已取消。
dnsPromises API 也会导出上述错误代码,例如,dnsPromises.NODATA。
【The dnsPromises API also exports the above error codes, e.g., dnsPromises.NODATA.】
实现的注意事项#>
【Implementation considerations】
虽然 dns.lookup() 和各种 dns.resolve*()/dns.reverse() 函数的目标都是将网络名称与网络地址关联(或反之),但它们的行为却有很大不同。这些差异可能对 Node.js 程序的行为产生微妙但重要的影响。
【Although dns.lookup() and the various dns.resolve*()/dns.reverse()
functions have the same goal of associating a network name with a network
address (or vice versa), their behavior is quite different. These differences
can have subtle but significant consequences on the behavior of Node.js
programs.】
dns.lookup()#>
在底层,dns.lookup() 使用与大多数其他程序相同的操作系统功能。例如,dns.lookup() 几乎总是以与 ping 命令相同的方式解析给定名称。在大多数类似 POSIX 的操作系统上,可以通过修改 nsswitch.conf(5) 和/或 resolv.conf(5) 中的设置来改变 dns.lookup() 函数的行为,但修改这些文件会改变在同一操作系统上运行的所有其他程序的行为。
【Under the hood, dns.lookup() uses the same operating system facilities
as most other programs. For instance, dns.lookup() will almost always
resolve a given name the same way as the ping command. On most POSIX-like
operating systems, the behavior of the dns.lookup() function can be
modified by changing settings in nsswitch.conf(5) and/or resolv.conf(5),
but changing these files will change the behavior of all other
programs running on the same operating system.】
尽管从 JavaScript 的角度来看,调用 dns.lookup() 是异步的,但它实际上是作为对 getaddrinfo(3) 的同步调用实现的,并在 libuv 的线程池上运行。这对于某些应用可能会产生令人意外的负面性能影响,更多信息请参见 UV_THREADPOOL_SIZE 文档。
【Though the call to dns.lookup() will be asynchronous from JavaScript's
perspective, it is implemented as a synchronous call to getaddrinfo(3) that runs
on libuv's threadpool. This can have surprising negative performance
implications for some applications, see the UV_THREADPOOL_SIZE
documentation for more information.】
各种网络 API 会在内部调用 dns.lookup() 来解析主机名。如果这是一个问题,可以考虑使用 dns.resolve() 将主机名解析为地址,并使用地址而不是主机名。此外,一些网络 API(例如 socket.connect() 和 dgram.createSocket())允许替换默认解析器 dns.lookup()。
【Various networking APIs will call dns.lookup() internally to resolve
host names. If that is an issue, consider resolving the host name to an address
using dns.resolve() and using the address instead of a host name. Also, some
networking APIs (such as socket.connect() and dgram.createSocket())
allow the default resolver, dns.lookup(), to be replaced.】
dns.resolve()、dns.resolve*() 和 dns.reverse()#>
【dns.resolve(), dns.resolve*(), and dns.reverse()】
这些函数的实现方式与 dns.lookup() 完全不同。它们不使用 getaddrinfo(3),并且它们总是在网络上执行 DNS 查询。这种网络通信总是异步进行的,并且不使用 libuv 的线程池。
【These functions are implemented quite differently than dns.lookup(). They
do not use getaddrinfo(3) and they always perform a DNS query on the
network. This network communication is always done asynchronously and does not
use libuv's threadpool.】
因此,这些函数不会像 dns.lookup() 那样对在 libuv 线程池上进行的其他处理产生负面影响。
【As a result, these functions cannot have the same negative impact on other
processing that happens on libuv's threadpool that dns.lookup() can have.】
它们不使用 dns.lookup() 使用的同一组配置文件。例如,它们不使用来自 /etc/hosts 的配置。
【They do not use the same set of configuration files that dns.lookup()
uses. For instance, they do not use the configuration from /etc/hosts.】