Example Client using Net module
Posted: Fri Oct 23, 2015 9:02 am
This example use the Net module to fetch a text file on my server.
The server is hosted in a cluster (I think that is what is called, when several domains are hosted with the same IP number)
Therefore it is not enough to just use an IPnumber.
The server is hosted in a cluster (I think that is what is called, when several domains are hosted with the same IP number)
Therefore it is not enough to just use an IPnumber.
Code: Select all
ESP8266WiFi.init();
var net = require('net');
var hostname="ernstc.dk";
ESP8266WiFi.getHostByName(hostname, function(ipAddress){
var client=net.connect({host: ESP8266WiFi.getAddressAsString(ipAddress), port:80}, function() {
print('Connected');
client.write('GET /esp.txt HTTP/1.1\r\nHost:' +hostname+'\r\nAccept: */*\r\n\r\n');
client.on('data', function(data) {
print(data);
});
client.on('close', function() {
print('Connection closed');
});
});
});