You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • In code in post #10

    function onPageRequest(req, res) {
      var a = url.parse(req.url, true);
      res.writeHead(200, { 'Content-Type': 'text/html' });
      res.end(page);
      if (a.query) b = a.query;
    }
    

    I would change the sequence to do the request processing first (lines 2 and 5), and then the response (lines 3 and 4):

    function onPageRequest(req, res) {
      var a = url.parse(req.url, true);
      if (a.query) b = a.query;
     // ...more of request processing
      res.writeHead(200, { 'Content-Type': 'text/html' });
      res.end(page);
    }
    

    On a side note: Using single quote (') instead of double quotes (") for coding html fragments as literal Strings makes life much easier (and code much more legible): you do not need to escape the double quotes ("). Furthermore, you do not need the carriage returns and line feeds / new lines (/r, /n) in html source. This helps you preserve memory of which you are usually anyway in short supply... last but not least, mutli-line literal strings that recently became available helps even more with legibility: ;)

    var page = '<!DOCTYPE html><html><body>
    <form action="" method="post">
    First name: <input type="text" value="" name="fname"><br>
    Last name: <input type="text" value="" name="lname"><br>
    <input type="submit" value="Submit"></form>
    <p>Click on the submit button, and the input will be sent to Espruino.</p>
    </body></html>';
    

    In 1st code in post #11:
    if it is not a GET, you cannot assume that it is a POST since there are other valid http verbs/methods. Interestingly is that - last time I checked - request.method returns for method (erroneously) always "GET". Only parsed url returns the actual method!

    For some GET/POST discussion, take a look at Testing SW from Espruino book, ....code from page 246 (Espruino Pico+ESP8266).

About

Avatar for allObjects @allObjects started