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