dns.lookup()


在幕后,dns.lookup() 使用与大多数其他程序相同的操作系统设施。 例如,dns.lookup() 几乎总是以与 ping 命令相同的方式解析给定名称。 在大多数类似 POSIX 的操作系统上,可以通过更改 nsswitch.conf(5) 和/或 resolv.conf(5) 中的设置来修改 dns.lookup() 函数的行为,但更改这些文件将更改在同一操作系统上运行的所有其他程序的行为。

尽管对 dns.lookup() 的调用从 JavaScript 的角度来看是异步的,但它被实现为对运行在 libuv 线程池上的 getaddrinfo(3) 的同步调用。 这可能会对某些应用程序产生令人惊讶的负面性能影响,有关更多信息,请参阅 UV_THREADPOOL_SIZE 文档。

各种网络 API 将在内部调用 dns.lookup() 来解析主机名。 如果这是一个问题,则考虑使用 dns.resolve() 并使用地址而不是主机名将主机名解析为地址。 此外,某些网络 API(例如 socket.connect()dgram.createSocket())允许替换默认解析器 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.

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.

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.