dnsPromises.lookup(hostname[, options])


  • hostname <string>

  • options <integer> | <Object>

    • family <integer> 记录族。必须是 460。值 0 指示返回 IPv4 或 IPv6 地址。如果值 0{ all: true } 一起使用(见下文),则返回 IPv4 和 IPv6 地址之一或两者,具体取决于系统的 DNS 解析器。默认值:0

      ¥family <integer> The record family. Must be 4, 6, or 0. The value 0 indicates that either an IPv4 or IPv6 address is returned. If the value 0 is used with { all: true } (see below), either one of or both IPv4 and IPv6 addresses are returned, depending on the system's DNS resolver. Default: 0.

    • hints <number> 一个或多个 支持的 getaddrinfo 标志。多个标志可以通过按位 OR 其值来传入。

      ¥hints <number> One or more supported getaddrinfo flags. Multiple flags may be passed by bitwise ORing their values.

    • all <boolean> 当为 true 时,Promise 使用数组中的所有地址进行解决。否则,返回单个地址。默认值:false

      ¥all <boolean> When true, the Promise is resolved with all addresses in an array. Otherwise, returns a single address. Default: false.

    • order <string> 当为 verbatim 时,Promise 使用按照 DNS 解析器返回的顺序使用 IPv4 和 IPv6 地址进行解决。当为 ipv4first 时,IPv4 地址位于 IPv6 地址之前。当 ipv6first 时,IPv6 地址位于 IPv4 地址之前。默认值:verbatim(地址未重新排序)。默认值可使用 dns.setDefaultResultOrder()--dns-result-order 进行配置。新代码应使用 { order: 'verbatim' }

      ¥order <string> When verbatim, the Promise is resolved with IPv4 and IPv6 addresses in the order the DNS resolver returned them. When ipv4first, IPv4 addresses are placed before IPv6 addresses. When ipv6first, IPv6 addresses are placed before IPv4 addresses. Default: verbatim (addresses are not reordered). Default value is configurable using dns.setDefaultResultOrder() or --dns-result-order. New code should use { order: 'verbatim' }.

    • verbatim <boolean> 当为 true 时,Promise 使用按照 DNS 解析器返回的顺序使用 IPv4 和 IPv6 地址进行解决。当为 false 时,IPv4 地址位于 IPv6 地址之前。此选项将被弃用,取而代之的是 order。当两者都指定时,order 具有更高的优先级。新代码应仅使用 order。默认值:目前是 false(地址已重新排序),但预计在不久的将来会发生变化。默认值可使用 dns.setDefaultResultOrder()--dns-result-order 进行配置。

      ¥verbatim <boolean> When true, the Promise is resolved with IPv4 and IPv6 addresses in the order the DNS resolver returned them. When false, IPv4 addresses are placed before IPv6 addresses. This option will be deprecated in favor of order. When both are specified, order has higher precedence. New code should only use order. Default: currently false (addresses are reordered) but this is expected to change in the not too distant future. Default value is configurable using dns.setDefaultResultOrder() or --dns-result-order.

将主机名(例如 'nodejs.org')解析为第一个找到的 A (IPv4) 或 AAAA (IPv6) 记录。所有 option 属性都是可选的。如果 options 是整数,则它必须是 46 – 如果未提供 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 是具有属性 addressfamily 的对象数组进行解决。

¥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() 不一定与域名系统协议有任何关系。该实现使用一种操作系统工具,可以将名称与地址关联起来,反之亦然。这种实现会对任何 Node.js 程序的行为产生微妙但重要的影响。在使用 dnsPromises.lookup() 之前,请花一些时间查阅 实现的注意事项章节

¥dnsPromises.lookup() does not necessarily have anything to do with the DNS protocol. The implementation uses an operating system facility that can associate names with addresses and vice versa. This implementation can have subtle but important consequences on the behavior of any Node.js program. Please take some time to consult the Implementation considerations section before using 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}]
});