• @Gordon
    Thank you, thank you, thank you ... that explains it all ... based on your pointer, I found the following excellent document on how Node.js handles HTTP transactions:

    https://nodejs.org/en/docs/guides/anatomĀ­y-of-an-http-transaction/

    What you say makes perfect sense now and is (in my opinion) the correct implementation. I'm making notes for what might be a programmer's guide to using Espruino and will cover this area. I had mistakenly assumed that Espruino would have received the whole HTTP transaction before invoking the HTTP server side callback ... but now I understand all it would have to do is receive the HTTP headers before invoking the callback and then the callback has a "stream" reader on the rest of the data. That data may or may not have arrived at the time when the callback is made.

    ... later ...
    For completeness, and it is likely most users will know this ... but if we want to process a complete transaction of data, then we can register a callback for the "close" event of the request at which time both available() and read() will be honored to return the total length and all the data. For example:

      var httpServer = http.createServer(function(request, response) {
        console.log("Received a server request!" + JSON.stringify(request));
        request.on("close", function() {
          console.log("The request has completed sending its data!");
          console.log("Available data is: " + request.available());
          var data = request.read();
          console.log("The data is: " + data);
        });
        response.write("Response data!");
        response.end();
      });
      httpServer.listen(80);
    
About

Avatar for Kolban @Kolban started