So What is node js?
Is it a web server? a framework? a language? what is it?
Well, it is not exactly a web server or a language. it is a java script runtime. But you can also develop a web server using node js.
NodeJs is a java script run-time based on chrome’s V8 java script engine.
It is a non blocking input output ( I/O ) model, which uses call backs to handle responses back and forth.
Callback is working on the basis of “Don’t wait for/call me, I will call you when I am done” concept..We will see more to it.
Node Js in short
- Event driven (uses event loop model)
- Non Blocking IO
- run-time environment or eco system
It is NOT a new programming language; Java script is used as a programming language to develop node.js applications.
Node.js is used to develop a web server normally, but it could be much more than that.
It’s ideal for web applications that are frequently accessed but doing simple computational operations.
What is blocking IO or typical synchronous approach of web applications?
Please check the diagram below:
Blocking IO model, the incoming requests to the server are processed one by one or synchronously. It means when the request is completed, the next will be processed so the other incoming requests should wait for their turn.
It is called ‘Blocking IO or synchronous IO’.
Non-Blocking IO or asynchronous IO:
In non blocking IO or asynchronous IO model, the requests are processed asynchronously. No one is waiting for the others to finish their task. each and every one is served independently.
In node.js the event loop is taking care of processing incoming requests without blocking(asynchronous). Non blocking IO is working based on call back methods, so when the process is completed, the call back will be called saying, it is done. please have your response.
Node.js is async and does not block resources for other requests.
Node.js Architecture and event loop:
Node.js takes a different approach by serving all requests from one single thread. The program code running in this thread is still executed synchronously but every time a system call takes place it will be delegated to the event loop along with a callback function.
The main process will not be put to sleep and will continue serving other requests. As soon as the previous system call is completed, the event loop will execute the callback passed. This callback will usually deal with the result returned and continue the program flow.
You can install node.js in your machine and play around with REPL.
Yes what is REPL? REPL – read eval print loop – a command line interface to play around and do some operations with node.js.
Please check my post on getting started with node.js simple program.
In the command prompt type ‘node’ enter, you can go to REPL:
Next in the Node.js series, we will look into what is event loop? and how node.js handles concurrent requests with Single-Threaded model. Please check my post on getting started with node.js simple program.