If you use this with hostname http://www.eecs.mit.edu, you get the correct ip-number 18.62.0.96.
A big thanks to the developers for this
ESP8266WiFi.init();
var hostname="www.eecs.mit.edu";
ESP8266WiFi.getHostByName(hostname, function(data){
print(ESP8266WiFi.getAddressAsString(data));
});
So, to fetch the indexpage:
ESP8266WiFi.init();
var hostname="www.eecs.mit.edu";
var ipn;
ESP8266WiFi.getHostByName(hostname, function(data){
print(ESP8266WiFi.getAddressAsString(data));
ipn=ESP8266WiFi.getAddressAsString(data);
});
var http = require("http");
http.get({
host: ipn,
port: 80,
path: "/"
}, function(response) {
print("get callback!");
response.on('data', function(data) {
print(data);
});
response.on('close', function() {
print("The response connection closed");
});
});
So when we call
ESP8266WiFi.getHostByName("www.google.com", function(ipAddress) {
// Do something with the ip address
});
Then the ipAddress isn't known until the callback returns.
If we code globals:
var myDesiredIp;
ESP8266WiFi.getHostByName("www.google.com", function(ipAddress) {
myDesiredIp = ipAddress;
});
// (A) ... do some code here
Then what happens is that a request to get the IP address is made and then immediately we reach point (A) in the execution. However, this does not mean that the callback from getHostByName has been executed. It might ... but it is as likely as not to not have been ... in which case "myDesiredIp" will be currently unset.
In JavaScript, using globals is not necessarily the right way to go. Instead ... we would want some code similar to:
ESP8266WiFi.getHostByName("www.google.com", function(ipAddress) {
// (A) ... do some code here
});
The philosophies of JavaScript are different from those of procedural oriented languages like C.