abhi9393 abhi9393 - 1 year ago 87
Javascript Question

How to resolve hostname to an ip address in node js

I need to resolve hostname defined in hosts file to its corresponding IP address.

For example my host file look like this - "/etc/hosts" ggns2dss81 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6 abcdserver testwsserver

Now in my
, i can read content of this file, but i need to fetch for given

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

PS -
npm pkg
or any third party package cannot be installed on machine.

Help is much appreciated!!

Answer Source
const dns = require('dns')

dns.lookup('testwsserver', function(err, result) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download