Ch-2 Introduction To Node JS
Ch-2 Introduction To Node JS
Introduction to Node JS
Contents
• Introduction
• What is Node JS and its advantages
• Traditional Web Server Model
• Node JS Process model
• Installation of Node JS
• Node JS event loop
• Javascript engine converts your javascript code into machine code.
• Different browsers use different Engines.
• E.g
• Netscape was using spidermonkey
• Internet explorer –chakra
• Chrome has build its own engine called v8.
What is Node.js?
•Open source: Node.js has an open source community which has produced man
y excellent modules to add additional capabilities to Node.js applications.
•License: Node.js is released under the MIT license.
"lightweight" generally refers to a system or process that requires minimal system resources and processing power.
Advantages of Node.js
• Event loop is an endless loop, which waits for tasks, executes them
and then sleeps until it receives more tasks.
• The event loop executes tasks from the event queue only when the
call stack is empty i.e. there is no ongoing task.
• The event loop allows us to use callbacks and promises.
• The event loop executes the tasks starting from the oldest first.
Example
console.log("This is the first statement");
setTimeout(function(){
console.log("This is the second statement");
}, 1000);
Output:-
This is the first statement
This is the third statement
This is the second statement
Explanation:
• In the above example, the first console log statement is pushed to the call
stack and “This is the first statement” is logged on the console and the task is
popped from the stack.
• Next, the setTimeout is pushed to the queue and the task is sent to the
Operating system and the timer is set for the task.
• This task is then popped from the stack. Next, the third console log statement
is pushed to the call stack and “This is the third statement” is logged on the
console and the task is popped from the stack.
• When the timer set by setTimeout function (in this case 1000 ms) runs out, the
callback is sent to the event queue. The event loop on finding the call stack
empty takes the task at the top of the event queue and sends it to the call
stack.
• The callback function for setTimeout function runs the instruction and
“This is the second statement” is logged on the console and the task
is popped from the stack.
• Note: In the above case, if the timeout was set to 0ms then also the
statements will be displayed in the same order. This is because
although the callback with be immediately sent to the event queue,
the event loop won’t send it to the call stack unless the call stack is
empty i.e. until the provided input script comes to an end.
Working of the Event loop:
• When Node.js starts, it initializes the event loop, processes the provided input script
which may make async API calls, schedule timers, then begins processing the event loop.
• When using Node.js, a special library module called libuv is used to perform async
operations.
• This library is also used, together with the back logic of Node, to manage a special thread
pool called the libuv thread pool.
• This thread pool is composed of four threads used to delegate operations that are too
heavy for the event loop.
• I/O operations, Opening and closing connections, setTimeouts are the example of such
operations.
• When the thread pool completes a task, a callback function is called which handles the
error(if any) or does some other operation. This callback function is sent to the event
queue. When the call stack is empty, the event goes through the event queue and sends
the callback to the call stack.
Phases of the Event loop:
Event loop in a Node.js server:
Now, if you make any changes in the "main.js" file, you need to again run the "node main.js"
command.