Strange behaviour of http.createServer

Posted on
  • Attached code runs fine, but after moving everything into an object, by removing comments, fails with this error.

    *Uncaught Error: Function "listen" not found!
    at line 13 col 10

      }).listen(8080);*
    

    Got this problem on ESP32, could not test on other board. @Gordon, if this is an ESP32 only problem, please move it to that category :-)
    Tested this 10 times with both options and always got same result.

    var wifi = require("Wifi");var http = require("http");
    var ssid = "xxxxxxx";
    var password = "yyyyyyyyyyyy";
    
    //function httpSrv(){
      function init(){
        var x;
        wifi.connect(ssid, {password: password}, function(dt) {
          http.createServer(function(req,res){
            res.writeHead(200);
            res.end("Hello World");
          }).listen(8080);
        });
      }
      init();
    //}
    //var s = new httpSrv();
    
    
  • It fails on linux too.

    It's because http.createServer returns an object of class httpSrv. If you then overwrite that built-in class with your own function, magically all the built-in methods of httpSrv disappear.

    It's not a bug, it's just part of JS. Rename httpSrv to httpServer and it'll be fine :)

  • Sometimes life can be so easy, once you know how to live.
    Thanks a lot, a "many-hours-without-success-story" comes to an end ;-)

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

Strange behaviour of http.createServer

Posted by Avatar for JumJum @JumJum

Actions