What is Node
How to Install Node
Building a Hello World App
Node Modules and Node Package Manager
File System in Node
Creating Web Server Using Express
Node and Databases
How Node Works
The original version has 8,000 lines of code (LOC)—and then, the standard libraries for programmers. The following is the figure of Node.js architecture:
The developers used the just-in-time (JIT) compiler methodology to improve the code compilation time. It is open source and is used in the Node.js and MongoDB project.
Event driven I/O – libuv
The libuv library is a cross-platform library that provides an asynchronous I/O facility by enabling an event-driven I/O operation. The libuv library creates a thread for the I/O operation (file, DNS, HTTP, and so on) and returns callback.
Upon completion of the particular I/O operation, it returns the events so that the callee program does not have to wait for the completion of I/O operation. We will see more about libuv in the upcoming sections.
Working of libuv – core of Node.js
As we mentioned above, libuv assigns threads for the I/O operation and returns the callback to the callee program.
Therefore, Node.js internally creates threads for I/O operation; however, it gives the programmer access to a single runtime thread. In this way, things are simple and sweet:
When you make an HTTP request to the web server running over Node.js. It creates the libuv thread and is ready to accept another request. As soon as the events are triggered by libuv, it returns the response to the user.
The libuv library provides the following important core features:
- Fully featured event loop
- Asynchronous filesystem operations
- Thread pool
- Thread and synchronization primitives
- Asynchronous TCP and UDP sockets
- Child process
- Signal handling
The libuv library internally uses another famous library called libeio, which is designed for threading and asynchronous I/O events and libev, which is a high-performance event loop. Therefore, you can treat libuv as a package wrapper for both of them.
In the next lesson, we will learn about the single threading programming model.