Node.js – dnsPromises.lookupService() 方法
dns.lookupService() 方法解析给定的地址和端口成一个主机名称和服务。此方法使用操作系统底层的 getnameinfo 实现。
如果地址无效 IP 地址,将引发 TypeError。dnsPromises 和 dns 模块的区别在于,dnsPromises 提供了一个替代异步 DNS 方法,该方法返回 Promise 对象而不是回调。
语法
dnsPromises.lookupService(address, port)
参数
address – 此参数接收需要解析的 IP 地址。
port – 此参数接收与 IP 地址关联的端口号。
示例 1
创建一个名为 "lookupService.js" 的文件并复制以下代码片段。创建文件后,使用命令 "node lookupService.js" 运行此代码。
// dns.lookupService() Demo Example
// Importing the dns module
const dns = require('dns');
const dnsPromises = dns.promises;
// Passing the IP address and port
dnsPromises.lookupService('127.0.0.1', 22).then((response) => {
console.log(response.hostname, response.service);
});输出
localhost ssh
示例 2
// dns.lookupService() Demo Example
// Importing the dns module
const dns = require('dns');
const dnsPromises = dns.promises;
// Passing the below options to lookup
const options = {
//IPv4
family: 4,
hints: dns.ADDRCONFIG | dns.V4MAPPED,
};
dnsPromises.lookup('tutorialspoint.com', options).then((response) =>
{
if(response){
console.log(response);
// Passing dns value to lookupservice
dnsPromises.lookupService(response.address, 80).then((res) => {
console.log(res.hostname, res.service);
});
}
});输出
C:\home
ode>> node lookupService.js { address: '95.217.74.146', family: 4 } static.146.74.217.95.clients.your-server.de http
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP