I'm trying to dynamically add IP address for a given host name.
code snippet
// This function will return me ip address of my server
dns.lookup('testwsserver', function (err, result {
hostIP = result;
console.log("Inside : "+hostIP); // Log 1
});
console.log("Outside : "+hostIP); // Log 2
var options = {
host : hostIP,
port : '8080',
path : null,
method : 'POST',
};
console.log(options); // Log 3
Above code is simply fetching IP address for given hostname and assigning it to variable "hostIP", issue is that i'm getting null value in hostIP when displaying outside of loop or using in options.
Output -
Outside : null // Log 2
{ hostname: null, // Log 3
port: '8080',
path: null,
method: 'POST',
}
Inside : 192.168.253.18 // Log 1
According to my need, the code should execute in order, first lookup function should assign value to hostIP and then rest execution.
Any help is appreciated!!
As you said node.js is async you have to do as follows:
// This function will return me ip address of my server
dns.lookup('testwsserver', function (err, result {
hostIP = result;
console.log("Inside : "+hostIP); // Log 1
console.log("Inside1 : "+hostIP); // Log 2
var options = {
host : hostIP,
port : '8080',
path : null,
method : 'POST',
};
console.log(options); // Log 3
});