You are reading a single comment by @Bunny and its replies. Click here to read the full conversation.
  • Hi,

    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"]=="applicatio­n/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_wp­a2"}, (error) => {
          if (error) throw error;
          console.log("Wifi  UP!, my IP is "+ wifi.getAPIP().ip);
          httpServer.createServer(onRequest).liste­n(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).liste­n(80); its works. What's happening with httpServer and why doesnt run at start?

    Thanks a lot

    Bunny

About

Avatar for Bunny @Bunny started