Please help me with this trouble, i tried to start an http server on a ESP8266 (Nodemcu v3) with this code:
function handlePOST(req, callback) {
var data = "";
req.on('data', function(d) { data += d; });
req.on('end', function() {
// All data received from the client, so handle the url encoded data we got
// If we used 'close' then the HTTP request would have been closed and we
// would be unable to send the result page.
let postData = {};
data.split("&").forEach(function(el) {
var els = el.split("=");
postData[els[0]] = decodeURIComponent(els[1]);
});
// finally our data is in postData
console.log(postData);
// do stuff with it!
console.log("We got the text on codeJS", postData.codeJS);
// call our callback (to send the HTML result)
callback(postData.codeJS);
});
}
function onRequest(req, res) {
var rurl = url.parse(req.url,true);
if (rurl.pathname=="/") {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(`<h1>Please send commands on HTTP POST at /cmd URL</h1>`);
} else if (rurl.pathname=="/cmd" && req.method=="POST" && req.headers["Content-Type"]=="application/x-www-form-urlencoded") {
handlePOST(req, (codeJS) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
let value = "";
value = eval(codeJS);
res.end(value);
});
} else {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end("<h1>404: Page "+rurl.pathname+" not found</h1>");
}
}
function onInit() {
carro.iniciarCarro();
let wifi = require("Wifi");
let httpServer = require("http");
wifi.disconnect();
wifi.setHostname("mycar");
wifi.startAP("wifi-me", {password:"my-password",authMode:"wpa_wpa2"}, (error) => {
if (error) throw error;
console.log("Wifi UP!, my IP is "+ wifi.getAPIP().ip);
httpServer.createServer(onRequest).listen(80);
wifi.save();
save();
});
}
But its doesnt start the http server on port 80. When connect to telnet at port 23, and run httpServer.createServer(onRequest).listen(80); its works. What's happening with httpServer and why doesnt run at start?
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hi,
Please help me with this trouble, i tried to start an http server on a ESP8266 (Nodemcu v3) with this code:
But its doesnt start the http server on port 80. When connect to telnet at port 23, and run
httpServer.createServer(onRequest).listen(80);
its works. What's happening with httpServer and why doesnt run at start?Thanks a lot
Bunny