Node.js architecture

The web application normally follows three tier web architecture consisting of client, web server, and data source. In our context, we have created a web app server using Node.js. As we discussed in chapter 1, Welcome to JavaScript in The Full Stack, Node.js follows a single threaded architectural model. To reduce the memory leak and understand asynchronousity while writing the code, we need to understand how Node.js works.

The following diagram depicts a visual representation of code:

Every processing component is sequenced in the following order"

  1. The client sends a request (consider an HTTP request).
  2. The Chrome's v8 engine is a just-in-time (JIT) compiler. Once the request is received at the server, v8 converts the JavaScript code to the machine code.
  3. The C++ APIs within the Node.js core provide a binding for other system-level components. Binding is basically a wrapper library so that a code written in one language can communicate with a code written in another language. This API is responsible for emitting an event.
  4. Once the event is emitted, it is stored in the event queue.
  5. The eventloop is responsible for fetching the event from the queue and executing it in the callstack.
  1. If an event requires an asynchronous operation to be done, such as using database files, it switches its execution context to another worker thread and gets executed. This is done by libuv. The libuv library is responsible for handling the asynchronous behavior of an event life cycle in the system. It is written in C. It maintains a pool of threads to handle asynchronous requests such as I/O and network-related operations.
  2. Once the asynchronous operation is completed, it returns the callback. The callback remains in the event queue until the callstack gets empty.
  3. Once the callstack is empty, the eventloop pulls the callback from the event queue and executes it in the callstack.
  4. Eventually, the event returns the data to the Node API.
  5. In each loop, it performs a single operation. Though the operations are performed sequentially, this single-threaded mechanized eventloop is so fast that it provides an illusion of concurrency. A single thread can utilize a single core of the system; hence, it provides better performance and minimal response time to client.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.181