• Here is the web server example:

    var fs = new(require("FlashStoreWrite"))(0x7c000);
    
    //fs.item('/images/logo.png').delete();
    
    fs.item('/favicon.ico').wget('http://www.espruino.com/favicon.ico');
    fs.item('/images/logo.png').wget('http://www.espruino.com/images/logo.png');
    
    function Doc() {
        this.str='';
    }
    
    Doc.prototype.write=function(s) {
        this.str+=s;
      this.str+='\n';
    };
    
    Doc.prototype.toString=function(){return this.str;};
    
    var document=new Doc();
    
      
    // http://www.accessify.com/tools-and-wizards/developer-tools/html-javascript-convertor/
    document.write("<html lang=\"en\">");
    document.write("  <head>");
    document.write("    <meta charset=\"utf-8\">");
    document.write("    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">");
    document.write("    <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">");
    document.write("    <link rel=\"stylesheet\" href=\"http:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.5\/css\/bootstrap.min.css\">");
    document.write("  <\/head>");
    document.write("  <body>");
    document.write("    <div class=\"well well-lgt\">  ");
    document.write("      <div class=\"panel-heading\">");
    document.write("        <h3>Web Flash Server<\/h3>");
    document.write("      <\/div>");
    document.write("      <button class=\"btn btn-warning\" id=\"send\" onclick=\"action(this);\">Send to Espruino<\/button>");
    document.write("      <ul id=\"list\" class=\"list-group\">");
    document.write("        <li class=\"list-group-item\"><\/li>");
    document.write("        <li class=\"list-group-item\"><img src=\"\/images\/logo.png\"><\/li>");
    document.write("      <\/ul>");
    document.write("    <\/div>");
    document.write("    <script src=\"\/js\/app.js\"><\/script>");
    document.write("  <\/body>");
    document.write("<\/html>");
      
    console.log( document.toString() );
    
    fs.item('/',document.toString(),'text/html');
    
    document.str='';
    
    document.write("var count=1;");
    document.write("function action(btn) {");
    document.write("  btn.innerHTML = 'Send ' + count;");
    document.write("  send_json(count);");
    document.write("    count = count + 1;");
    document.write("}");
    document.write("function add(text) {");
    document.write("  var ul = document.getElementById(\"list\");");
    document.write("  var li = document.createElement(\"li\");");
    document.write("  li.className = \"list-group-item\";");
    document.write("  li.appendChild(document.createTextNode(text));");
    document.write("  ul.appendChild(li);");
    document.write("}");
    document.write("function send_json(count) {");
    document.write("  count = count + 1;");
    document.write("  var xhttp = new XMLHttpRequest();");
    document.write("  xhttp.onreadystatechange = function () {");
    document.write("    if (xhttp.readyState == 4 && xhttp.status == 200) {");
    document.write("      add(xhttp.responseText);");
    document.write("    }");
    document.write("  };");
    document.write("  xhttp.open(\"GET\", \"\/json?count=\" + count, true);");
    document.write("  xhttp.send();");
    document.write("};");
    
    fs.item('/js/app.js',document.toString(),'application/javascript');
    
    print( process.memory());
    
    delete document;
    
    print( process.memory());
    
    var fs = new(require("FlashStore"))(0x7c000);
    
    require("http").createServer(function (request, response) {
        var u = url.parse(request.url, true);
        var q = fs.find(u.pathname);
        if (q) {
          console.log({match:u.pathname});
          q.pipe(response);
        } else {
          if ( u.pathname === '/json' ) {
             // response.writeHead(200);
            response.end(Date.now().toString());
            return;
          }
          console.log({   q : u.query,   p : u.pathname  });
          response.writeHead(404);
          response.end("404: Not found");
        }
    
      }).listen(80);
    print(process.memory());
    

    The webserver - using the FlashStore object to retrieve, it assumes you have a saved wifi.save() that is already connected:

    var fs = new(require("FlashStore"))(0x7c000);
    
    require("http").createServer(function (request, response) {
        var u = url.parse(request.url, true);
        var q = fs.find(u.pathname);
        if (q) {
          console.log({match:u.pathname});
          q.pipe(response);
        } else {
          if ( u.pathname === '/json' ) {
             // response.writeHead(200);
            response.end(Date.now().toString());
            return;
          }
          console.log({   q : u.query,   p : u.pathname  });
          response.writeHead(404);
          response.end("404: Not found");
        }
    
      }).listen(80);
    print(process.memory());
    

    The ico and image assets are saved, then the index.html root document and /js/app.js

    The webserver code searches the store, and if the content is matched it s served from the flash, using the .pipe method.

    When the button is clicked, a /json method is called, and this returning the current time back to the browser...


    2 Attachments

About

Avatar for Wilberforce @Wilberforce started