http server doesnt listen on 80 port

Posted on
  • 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
          // do stuff with it!
          console.log("We got the text on codeJS", postData.codeJS);
          // call our callback (to send the HTML result)
    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);
        } else {
            res.writeHead(404, {'Content-Type': 'text/html'});
            res.end("<h1>404: Page "+rurl.pathname+" not found</h1>");
    function onInit() {
      let wifi = require("Wifi");
      let httpServer = require("http");
      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);

    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


  • I found that I had to wait a few seconds after startup to do the createServer() call - try putting that in a 5 second timeout.

  • Please send me an code example.

  • remove save() from function onInit();

    If you want to save your code add this as last line:

  • Thanks a lot by the answers. Its works!

  • @MaBe, #4 is nice alternative to my suggestion of having this as last line

    setTimeout(onInit,1000); // while developing only

    and remove it before the last upload that I then finish with a save() in the console.

    I came up with my suggestion in order to write to the flash only when I need to keep wear and tear as low as possible... but this show only the wear-phoby of mine...

  • this is what i like to do

    testing = true ; // false;
    if (testing)
  • or like this ;-)

    testing = true ; // false;
    // upload start or save
    if (testing) {
            setTimeout(() => { onInit(); }, 1E3);
    } else {
            setTimeout(() => { save(); }, 1E3);
  • I see. You can then add even more testing-only things because you already have the testing variable available... and you change the set value before the 'last' upload after all is just working fine.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

http server doesnt listen on 80 port

Posted by Avatar for Bunny @Bunny