📜  Node.js DNS

📅  最后修改于: 2022-05-13 01:56:41.498000             🧑  作者: Mango

Node.js DNS

介绍:

DNS 是一个节点模块,用于执行操作系统提供的名称解析功能,也用于执行实际的 DNS 查找。

优势
无需记住 IP 地址——DNS 服务器提供了一个将域名或子域名转换为 IP 地址的绝妙解决方案。

示例 1:

// Include 'dns' module and create its object
const dns = require('dns');
  
const website = 'geeksforgeeks.org';
// Call to lookup function of dns
dns.lookup(website, (err, address, family) => {
  console.log('address of %s is %j family: IPv%s', 
           website, address, family);
});
  
// Execute using $ node 

输出:

address of geeksforgeeks.org is "52.25.109.230" family: IPv4

示例 2:

// Include 'dns' module and create its object
var dns = require('dns');
  
// Call to reverse function along with lookup function.
dns.lookup('www.geeksforgeeks.org', 
     function onLookup(err, address, family) {
    console.log('address:', address);
    dns.reverse(address, function (err, hostnames) {
      console.log('reverse for ' + address + ': ' 
             + JSON.stringify(hostnames));
   });  
});
  
// Execute using $ node 

输出:

address: 52.222.176.140
reverse for 52.222.176.140: ["server-52-222-176-140.bom52.r.cloudfront.net"]

参考:
https://nodejs.org/docs/latest-v9.x/api/dns.html#dns_dns