1v91 HTTP server callback called twice

Posted on
  • I have just got started using Espruino, it looks great, but I have come across a few issues. I don't know if it's my fault, but I have found that when I use the built in HTTP server the callback function is called twice. Once with the query parameters, and once (second time) with them nulled out. This

    I am using using a compiler to generate my JS, so it's slightly awkward but still readable. Here is the callback used by wifi.connect()

     var onConnected = function onConnected(err) {
        if (err == null) {
          console.log("IP: " + wifi.getIP().ip);
          var http = require("http");
          var callback = function callback(request, response) {
            var query = void 0;
            var matchValue = url.parse(request.url, true).query;
            if (matchValue == null) {
              query = null;
            } else {
              query = matchValue;
            response.write("<a href=\"?led=1\">on</a><br/><a href=\"?led=0\">off</a>");
            response.end("sup dawg?");
            console.log("Callback called");
            if (query != null) {
              var matchValue_1 = query.led;
              if (matchValue_1 === "1") {
                digitalWrite(outPutPin, 1);
              } else {
                digitalWrite(outPutPin, 0);
          var server = http.createServer(callback);
        } else {
          console.log("Error: " + err);

    log output from hitting the page once:

    Callback called
    Callback called

    The work around I'm using is just to only set the LEDs if the query is not null. What might be going wrong?


  • Maybe you could dump the contents of the request object at the same time? You usually get two requests from a browser - one for the requested page, and one for the favicon.

  • That sounds like a good explanation. I'm so used to using a router I overlooked that it would request a favicon too.


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

1v91 HTTP server callback called twice

Posted by Avatar for user75944 @user75944