4 Replies Latest reply on Nov 18, 2015 8:26 AM by Hathout

    My node JS web-server is too slow ??

    Hathout

      My webserver is running very slow

      here is my code!


       

      /**** indicate if Relays are on or off ****/

      var relay_1_status = "off";

      var relay_2_status = "off";

      var relay_3_status = "off";

      var relay_4_status = "off";

      var system = require('child_process').exec;

      var mraa = require('mraa');

      /*

      * About controlling galileo with a webpage

      */

      function startServer()

      {

        console.log("Loading..");

        var R1 = new mraa.Gpio(2);

        var R2 = new mraa.Gpio(3);

        var R3 = new mraa.Gpio(4);

        var R4 = new mraa.Gpio(5);

        R1.dir(mraa.DIR_OUT);

        R2.dir(mraa.DIR_OUT);

        R3.dir(mraa.DIR_OUT);

        R4.dir(mraa.DIR_OUT);

        console.log("Starting web controller...");

        var http = require('http');

        http.createServer(function (req, res) {

        function respond() {

        res.writeHead(200, { "Content-Type": "text/html" });

        res.write("<!DOCTYPE html><html><body>");

        res.write("<center><h1>OstaLab Final Project</h1></center>");

        res.write("</p>Switches Status : <p>");

        res.write(relay_1_status);

        res.write(relay_2_status);

        res.write(relay_3_status);

        res.write(relay_4_status);

        res.write("<p>Control your sweet home ^_^ : </p>");

        res.write("<p><input type='button' onclick='location.pathname = \"/S1On\"' value='Turn switch 1 on'/></p>");

        res.write("<p><input type='button' onclick='location.pathname = \"/S1Off\"' value='Turn switch 1 off'/></p>");

        res.write("<p><input type='button' onclick='location.pathname = \"/S2On\"' value='Turn switch 2 on'/></p>");

        res.write("<p><input type='button' onclick='location.pathname = \"/S2Off\"' value='Turn switch 2 off'/></p>");

        res.write("<p><input type='button' onclick='location.pathname = \"/S3On\"' value='Turn switch 3 on'/></p>");

        res.write("<p><input type='button' onclick='location.pathname = \"/S3Off\"' value='Turn switch 3 off'/></p>");

        res.write("<p><input type='button' onclick='location.pathname = \"/S4On\"' value='Turn switch 4 on'/></p>");

        res.write("<p><input type='button' onclick='location.pathname = \"/S4Off\"' value='Turn switch 4 off'/></p>");

        res.write("</body></html>");

          res.end();

        }

       

       

        console.log("Request: " + req.url);

       

       

      // Control L Switch 1

        if(req.url === "/S1On") {

        R1.write(0);

          relay_1_status = "on";

        console.log("pin 2 on");

        }

        if(req.url === "/S1Off") {

        R1.write(1);

          relay_1_status = "off";

        console.log("pin 2 off");

        }

       

       

      // Control L Swtich 2

       

       

        if(req.url === "/S2On") {

        R2.write(0);

          relay_2_status = "on";

        console.log("pin 3 on");

        }

        if(req.url === "/S2Off") {

        R2.write(1);

          relay_2_status = "off";

        console.log("pin 3 off");

        }

       

       

       

       

      // Control L Swtich 3

       

       

        if(req.url === "/S3On") {

        R3.write(0);

          relay_3_status = "on";

        console.log("pin 4 on");

        }

        if(req.url === "/S3Off") {

        R3.write(1);

          relay_3_status = "off";

        console.log("pin 4 off");

        }

       

       

       

       

      // Control L Swtich 4

       

       

        if(req.url === "/S4On") {

        R4.write(0);

          relay_4_status = "on";

        console.log("pin 5 on");

        }

        if(req.url === "/S4Off") {

        R4.write(1);

          relay_4_status = "off";

        console.log("pin 5 off");

        }

        respond();

       

        }).listen(1337);

        console.log('Server running at http://your_board_IP:1337/');

      }

       

       

      /*

      * What we want to run

      */

      console.log("Starting server");

      startServer();

       

       

       

      Any Suggestions ??