Node.js – dns.lookup() 方法
dns.lookup() 方法将主机名(例如,tutorialspoint.com)解析为找到的第一个 A(IPv4)或 AAAA(IPv6)记录。options 下可用的属性是可选的。dns.lookup() 与 DNS 协议无关。该实现使用一个操作系统工具,该工具可以将名称与地址关联起来,反之亦然。
语法
dns.lookup(hostname, [options], callback)
参数
以上参数定义如下:
hostname – 这是您要查找 DNS 值的网站主机名。
options – 它可以包含以下选项
family – 它只能取值 4、6 或 0。“0”表示返回 IPv4 和 IPv6 两个地址。
hints – 它启用一个或多个 getAddrinfoflags。
all – 当此值设置为 true 时,回调将所有解析的地址返回到数组中,否则返回单个地址。
verbatim – 当设置为 True 时,回调按 DNS 解析器返回的相同顺序返回。
callback – 它将捕获任何错误。
示例 1
创建一个名为 “lookup.js” 的文件,并复制以下代码片段。创建文件后,使用以下命令 “node lookup.js” 运行此代码。
// dns.lookup() method Demo Example
// Importing the dns module
const dns = require('dns');
// Passing some options for dns.lookup()
const options = {
family: 6,
hints: dns.ADDRCONFIG | dns.V4MAPPED,
};
dns.lookup('tutorialspoint.com', options, (err, address, family) =>
// This will display the address family and its value
console.log('address: %j family: IPv%s', address, family));输出
address: undefined family: IPvundefined
示例 2
// dns.lookup() method Demo Example
// Importing the dns module
const dns = require('dns');
// Initializing some options
const options = {
family: 6,
hints: dns.ADDRCONFIG | dns.V4MAPPED,
};
// Result will be an array, when all the options are true
options.all = true;
dns.lookup('tutorialspoint.com', options, (err, addresses) =>
console.log('addresses: %j', addresses));输出
addresses: undefined
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP