dns.lookupService(address, port, callback)


使用操作系统的底层 getnameinfo 实现将给定的 addressport 解析为主机名和服务。

如果 address 不是有效的 IP 地址,则会抛出 TypeErrorport 将被强制为数字。 如果不是合法的端口,则会抛出 TypeError

出错时,errError 对象,其中 err.code 是错误码。

const dns = require('node:dns');
dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
  console.log(hostname, service);
  // 打印: localhost ssh
});

如果此方法作为其 util.promisify() 版本被调用,则其将为具有 hostnameservice 属性的 Object 返回 Promise

Resolves the given address and port into a host name and service using the operating system's underlying getnameinfo implementation.

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.

On an error, err is an Error object, where err.code is the error code.

const dns = require('node:dns');
dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
  console.log(hostname, service);
  // Prints: localhost ssh
});

If this method is invoked as its util.promisify()ed version, it returns a Promise for an Object with hostname and service properties.