You are reading a single comment by @hansamann and its replies. Click here to read the full conversation.
  • Hi all,

    first of all let me know that I am super impressed by espruino. I got 4 kickstarter boards including the CC3000 adafruit wifi breakout and creating a server plus responding to requests and serving some JSON response etc. is in total super smooth - compared to other options out there.

    I have one question though. Below is the full createServer code, but the code snippet I really don't like right now is this:

    	  if(req.url.substr(0,2) == "/?")
          	digitalWrite(C15, (req.url.indexOf('led=true') != -1));
    

    based on a call to the root resource / I am determining whether to turn an LED on or off. To my knowledge, there is no automatic req.queryString object for example that would give you the queryString. Even better would be to get a pre-parsed map of the querystrings, then I could do:

    if (req.params.led == 'true')
     -> do stuff
    

    The way I found the url property is by JSON.stringifying the req object and that one is pretty large. I might have overlooked something, but I could not find more specific ways to access the path of the call or the queryString and parameters.

    Is this planned to be added or somewhere where I just did not find it?

    Thx
    Sven

    wlan.connect( "xxx", "xxx", function (s) { 
      if (s=="dhcp") {
        console.log("My IP is "+wlan.getIP().ip);
        require("http").createServer(function (req, res) {
          //turn LED on/off
          console.log(req.url);
          
    	  if(req.url.substr(0,2) == "/?")
          	digitalWrite(C15, (req.url.indexOf('led=true') != -1));
          
          var obj = {};
          obj.success = true;
          obj.poti = analogRead(A1);
          
          res.writeHead(200, {'Content-Type': 'application/json'});
          //res.write("{\"success\":true, \"val\":"+analogRead(A1)+"}");
          res.write(JSON.stringify(obj));
          res.end();
        }).listen(80);
      }
    }); 
    
About

Avatar for hansamann @hansamann started