Starting with Node.JS: Kickstarter project

So! Finally finished with Head First JS (yes, the entire book) and  now on to learning Node.JS.

What is Node.JS?
Node.JS is nothing but a JavaScript engine which executes JS outside a browser. The engine used to parse JS in Google Chrome is called Chrome V8. This was taken out of the browser and allowed to execute JS scripts in the OS environment, just like a Python interpreter or JVM. The resulting technology is called as Node.JS.

Building web applications with Node.JS
As the Node Beginner Book states, we can build a web app with Node.JS, using JS on server side, however, we have to write all parts from scratch, including the HTTP Server itself. If you have ever used PHP, then the HTTP server used was Apache, and you did not have to write logic to serve requests. This is different from Node.JS. Here, you have to write the request handling logic (though it can be performed by a framework).

Node is Single Threaded
If you have 100 different clients connected to a PHP web app, PHP will create 100 different processes, one per each client. This way, even if a single client starts a long, blocking application, the effect of that will be limited only to that client. Others will not realize it.
This is different with Node. It uses only a single process and thread to serve ALL clients. So, if a client starts an operation which is synchronous and takes 100 seconds, ALL the other clients will have to wait for it to complete -- without even knowing what they are waiting for! This is a curse as well as a boon. This is solved using asynchronous operations, which is the heart of JavaScript. We are going to use a lot of callback functions, so that Node does not wait for the operation to finish, instead it says: OK, this is a long operation and it's callback function that I have, I will let it execute in background with my other tasks, and once it's done, I'll call the callback function.

Starter project in Node.JS
So I read up the Node Beginner Book, ~55 pages. It describes a basic web app to create a modularized HTTP server and display the text that user entered through a form. I found the module design in that book slightly confusing and odd, and hence decided to come up with my own design for the problem. Accordingly I wrote this, have a look:
https://github.com/adityamedhe/NodeJS-BasicHttpServer

So we have 4 modules:
  1. app.js: This module is the main module. It uses the services of other modules.
  2. server.js: The HTTP Server. It takes in a Router object, and sends all the requests received to that object, for further processing. Handles all the request/response transactions.
  3. router.js: The request router. It contains a Routing table, which is nothing but a list of functions available to service a request. It receives a path requested from the server. If it finds a function with the same name as the path, it invokes that function, and returns it's result to the server, to be sent to the client. Else, it just returns null.
  4. requestHandlers.js: The file which contains all the request handling functions. If you want to add a function say abc() just define it here and declare it in the exports section of this file. Any requests to /abc will be invoking this function then.
That's it! A very basic, crude, HTTP server with request routing. Many flaws, I accept. That's why it's on GitHub, it will be continuously updated and improved. Let's do something exciting with the newly acquired knowledge!

3 comments:

  1. Nice One Aditya :) NodeJs is very good at the server side and its asynchronous nature is awesome. From last 6 months I have been working on this and I find that its very simple and easy to understand. So keep it up you will really enjoy with this. I think I'll be writing blog on this soon. :P

    ReplyDelete
    Replies
    1. Thanks Harshal! Can you let me know some good books to read while getting started with NodeJS? Looking forward to see your blog!

      Delete
    2. I referred following video. This may help you.
      https://www.youtube.com/watch?v=czmulJ9NBP0

      Delete