如何将主机名parsing为节点js中的一个IP地址

我需要将hosts文件中定义的主机名parsing为相应的IP地址。

例如我的主机文件看起来像这样 – “/ etc / hosts”

127.0.0.1 ggns2dss81 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 192.168.253.8 abcdserver 192.168.253.20 testwsserver 

现在在我的node.js ,我可以读取这个文件的内容,但我需要获取给定的hostname

 hostname = "testwsserver" hostIP = getIP(hostname); console.log(hostIP); // This should print 192.168.253.20 

PSnpm pkg或任何第三方软件包不能安装在机器上。

帮助非常感谢!

如何NodeJS文档 – DNS – 你检查它?

 const dns = require('dns') dns.lookup('testwsserver', function(err, result) { console.log(result) }) 
Interesting Posts