Node.js – dnsPromises.lookupService() 方法
该方法将给定的地址和端口解析为主机名和服务。此方法使用操作系统的底层getnameinfo实现。dns.lookupService()
一个类型错误,如果地址不是有效的IP地址将被抛出。dnsPromises和dns模块之间的区别在于dnsPromises提供了一种替代异步DNS方法的方法,这些方法返回Promise对象而不是回调。
语法
dnsPromises.lookupService(address, port)
参数
address–此参数接受需要解析的IP地址的输入。
port–此参数接受与IP地址相连的端口号的输入。
示例1
创建一个名为“lookupService.js”的文件并复制以下代码片段。创建文件后,使用命令“nodelookupService.js”运行此代码。
//dns.lookupService()演示示例 //导入dns模块 const dns = require('dns'); const dnsPromises = dns.promises; //传递IP地址和端口 dnsPromises.lookupService('127.0.0.1', 22).then((response) => { console.log(response.hostname, response.service); });输出结果
localhost ssh
示例2
//dns.lookupService()演示示例 //导入dns模块 const dns = require('dns'); const dnsPromises = dns.promises; //传递以下选项进行查找 const options = { //IPv4 family: 4, hints:dns.ADDRCONFIG| dns.V4MAPPED, }; dnsPromises.lookup('nhooo.com', options).then((response) => { if(response){ console.log(response); //将dns值传递给lookupservice dnsPromises.lookupService(response.address, 80).then((res) => { console.log(res.hostname, res.service); }); } });输出结果
C:\home\node>> node lookupService.js { address: '95.217.74.146', family: 4 } static.146.74.217.95.clients.your-server.de http