What is the architecture and workflow of Node.js?
Overview
Node.js is a platform built on a JavaScript foundation. It operates on the JavaScript V8 engine present in Google Chrome. It helps us develop web and single-page applications. Node.js is used by both large organizations as well as upcoming start-up companies.
Node.js architecture
Node.js architecture uses the 'single-threaded event loop' architecture for handling multiple client requests.
Parts of the Node.js architecture
- Node.js server: The Node.js server receives user requests, processes them, and sends feedback to the users. It is the server-side of the platform.
- Event queue: The event queue helps in request handling. It arranges incoming requests and sends them for processing one after the other.
- Requests: These are the tasks users try to perform on web applications. They are sent to the web application's server.
- Thread pool: These are the number of available threads used to carry out every incoming request by the user.
- Event loop: The event loop receives requests from clients, processes them, and sends them back to the client.
- External resources: These are resources that help perform client requests. The request could be computational or requires data storage.
Workflow in Node.js
- A request is sent to the server by the client. The request could be to delete data, query data, update data, and so on.
- The request is received by the event queue and kept for processing.
- The events are sent in the order they were received through the event loop. A check is also performed to know if any event requires external resources.
- The event loop processes the request (input/output) output polling and sends the feedback to the client.
Advantages of using Node.js
- Fewer resources are required in the operations carried out in the Node.js server.
- Node.js can handle multiple concurrent requests with ease. This is made possible through the event queue and the thread pool.
- Node.js does not need multiple threads because the event loop helps handle the events one after the other.