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


  • hostname <string>

  • options <integer> | <Object>

    • family <integer> | <string> 记录族。必须是 460。出于向后兼容的原因,'IPv4''IPv6' 分别被解释为 46。值 0 指示返回 IPv4 或 IPv6 地址。如果值 0{ all: true } 一起使用(见下文),则返回 IPv4 和 IPv6 地址之一或两者,具体取决于系统的 DNS 解析器。默认值:0

      ¥family <integer> | <string> The record family. Must be 4, 6, or 0. For backward compatibility reasons,'IPv4' and 'IPv6' are interpreted as 4 and 6 respectively. 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 时,回调返回数组中所有已解析的地址。否则,返回单个地址。默认值:false

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

    • order <string>verbatim 时,解析的地址未排序地返回。当 ipv4first 时,解析的地址按将 IPv4 地址置于 IPv6 地址之前的方式排序。当 ipv6first 时,解析的地址通过将 IPv6 地址置于 IPv4 地址之前进行排序。默认值:verbatim(地址未重新排序)。默认值可使用 dns.setDefaultResultOrder()--dns-result-order 进行配置。

      ¥order <string> When verbatim, the resolved addresses are return unsorted. When ipv4first, the resolved addresses are sorted by placing IPv4 addresses before IPv6 addresses. When ipv6first, the resolved addresses are sorted by placing IPv6 addresses before IPv4 addresses. Default: verbatim (addresses are not reordered). Default value is configurable using dns.setDefaultResultOrder() or --dns-result-order.

    • verbatim <boolean> 当为 true 时,回调按照 DNS 解析器返回的顺序接收 IPv4 和 IPv6 地址。当为 false 时,IPv4 地址位于 IPv6 地址之前。此选项将被弃用,取而代之的是 order。当两者都指定时,order 具有更高的优先级。新代码应仅使用 order。默认值:true(地址未重新排序)。默认值可使用 dns.setDefaultResultOrder()--dns-result-order 进行配置。

      ¥verbatim <boolean> When true, the callback receives 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: true (addresses are not reordered). Default value is configurable using dns.setDefaultResultOrder() or --dns-result-order.

  • callback <Function>

    • err <Error>

    • address <string> IPv4 或 IPv6 地址的字符串表示形式。

      ¥address <string> A string representation of an IPv4 or IPv6 address.

    • family <integer> 46,表示 address 族,如果地址不是 IPv4 或 IPv6 地址,则表示为 00 可能是操作系统使用的名称解析服务中存在错误的指示符。

      ¥family <integer> 4 or 6, denoting the family of address, or 0 if the address is not an IPv4 or IPv6 address. 0 is a likely indicator of a bug in the name resolution service used by the operating system.

将主机名(例如 '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 选项设置为 truecallback 的参数更改为 (err, addresses)addresses 是具有属性 addressfamily 的对象数组。

¥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.

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

¥dns.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 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() 版本被调用,并且 all 未设置为 true,则它为具有 addressfamily 属性的 Object 返回 Promise

¥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.