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 ssh
const dnsPromises = require('node:dns').promises;
dnsPromises.lookupService('127.0.0.1', 22).then((result) => {
console.log(result.hostname, result.service);
// Prints: localhost ssh
});