Thursday, October 22, 2020

An experiment using Node.js


An experiment with Node.js


Javascript is a scripting language originally used for bringing interactivity to web-pages. Javascript executes within an engine completely on the client side within a browser. Javascript is embedded in an HTML code and has access to the HTML elements. It can be used to respond to HTML buttons, validate forms, draw on canvas etc...


Now, Javascript engine (Google's version) has been embedded within a C++ program so that Javascript can be used as a console program. This is called Node.js. Among other things, Node.js can be used to implement an http server. Node has many libraries provided by built-in modules such as file, os, events and http. By default, calls are asynchrononous with Node using a single thread. This makes Node very efficient. Events allow to code asynchronous events with publish/subscribe model.


A simple RESTful server can be implemented using Node by looking at the http request method and the http request url. The response can contain client side Javascript code. The following is a simple experiement with Javascript on both server and client:


-----------------------------------------------------------------

app.js (Run as: node app.js)

-----------------------------------------------------------------

const mod1 = require('./MOD1');

const http = require('http');

const fs = require('fs');


mod1.log('FACT of 5 is ' + mod1.fact(5));

http.createServer((req, res)=>{

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

        res.write("<BODY style='color:red;'>");

        res.write(req.url);

        fs.readFile('a.txt', (err, data)=>{

            res.write(data);

            res.write("<form method='post' action='http://localhost:3000/QQQ'><input type='submit'/></form>");

            res.write("</BODY>");

           res.end();

        });

    } else {

        res.write("<BODY style='background-color:red;'>");

        res.write(req.url);

        fs.readFile('b.txt', (err, data)=>{

            res.write(data);

            res.write("<form method='post' action='http://localhost:3000/PPP'><input type='submit'/></form>");

            res.write("</BODY>");

           res.end();

        });


    }

}).listen(3000);


-----------------------------------------------------------------


Node allows user-defined modules such as MOD1.js:

-----------------------------------------------------------------


function log(message) {

    console.log(message);

}


function fact(n) {

    if (n <= 1) {

        return 1;

    }

    return n*fact(n-1);

}


exports.log = log;

exports.fact = fact;



-----------------------------------------------------------------

a.txt:

-----------------------------------------------------------------


<H1 style="color:blue;">

THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG!

</H1>

<canvas id="myCanvas" width="200" height="100"

style="border:1px solid #c3c3c3;">

Your browser does not support the canvas element.

</canvas>


<script>

function draw() {

    alert('a');

    var canvas = document.getElementById("myCanvas");

    var ctx = canvas.getContext("2d");

    ctx.fillStyle = "#FF0000";

    ctx.fillRect(0,0,150,75);

}


function clean() {

    alert('b');

    var canvas = document.getElementById("myCanvas");

    var ctx = canvas.getContext("2d");

    ctx.fillStyle = "#FFFFFF";

    ctx.fillRect(0,0,150,75);

}

</script>


<button onClick="draw();">DRAW</button><br/>

<button onClick="clean();">CLEAR</button><br/>


-----------------------------------------------------------------

b.txt:

----------------------------------------------------------------

A STITCH IN TIME SAVES NINE.