Explain the working of Node.js - GeeksforGeeks (2024)

Last Updated : 27 May, 2024

Summarize

Comments

Improve

Welcome to the world of Node.js, an open-source runtime environment that has transformed the landscape of backend development. Traditionally, JavaScript was confined for frontend development, powering user interactions on the browser. However, with the advent of Node.js, JavaScript has broken free from these chains and now reigns on the server-side as well, enabling full-stack development with a single language. Node.js runs on chrome v8 engine which converts javascript code into machine code, it is highly scalable, lightweight, fast, and data-intensive.

Working of Node.js

Node.js accepts the request from the clients and sends the response, while working with the request node.js handles them with a single thread. To operate I/O operations or requests node.js use the concept of threads. Thread is a sequence of instructions that the server needs to perform. It runs parallel on the server to provide the information to multiple clients. Node.js is an event loop single-threaded language. It can handle concurrent requests with a single thread without blocking it for one request.

Node.js basically works on two concept

  • Asynchronous
  • Non-blocking I/O

1. Non-blocking I/o

Non-blocking i/o means working with multiple requests without blocking the thread for a single request. I/O basically interacts with external systems such as files, databases. Node.js is not used for CPU-intensive work means for calculations, video processing because a single thread cannot handle the CPU works.

2. Asynchronous

Asynchronous is executing a callback function. The moment we get the response from the other server or database it will execute a callback function. Callback functions are called as soon as some work is finished and this is because the node.js uses an event-driven architecture. The single thread doesn’t work with the request instead it sends the request to another system which resolves the request and it is accessible for another request.

To implement the concept of the system to handle the request node.js uses the concept of Libuv.

Libuv is an open-source library built-in C. It has a strong focus on asynchronous and I/O, this gives node access to the underlying computer operating system, file system, and networking.

Libuv implements two extremely important features of node.js

  • Event loop
  • Thread pool

Event loop:

The event loop contains a single thread and is responsible for handling easy tasks like executing callbacks and network I/O. When the program is to initialize all the top-level code is executed, the code is not in the callback function. All the applications code that is inside callback functions will run in the event loop. EventLoop is the heart of node.js. When we start our node application the event loop starts running right away. Most of the work is done in the event loop.

Nodejs use event-driven-architecture.

  • Events are emitted.
  • Event loop picks them up.
  • Callbacks are called.

Event queue

As soon as the request is sent the thread places the request into a queue. It is known as an event queue. The process like app receiving HTTP request or server or a timer will emit event as soon as they are done with the work and event loop will pick up these events and call the callback functions that are associated with each event and response is sent to the client.

The event loop is an indefinite loop that continuously receives the request and processes them. It checks the queue and waits for the incoming request indefinitely.

Thread pool

Though node.js is single-threaded it internally maintains a thread pool. When non-blocking requests are accepted there are processed in an event loop, but while accepting blocking requests it checks for available threads in a thread pool, assigns a thread to the client’s request which is then processed and send back to the event loop, and response is sent to the respective client.

The thread pool size can be change:

process.env.UV_THREADPOOL_SIZE = 1;

Explain the working of Node.js - GeeksforGeeks (1)

Working of Node.js



Please Login to comment...

Explain the working of Node.js - GeeksforGeeks (2024)

FAQs

How does NodeJS work explain? ›

Node. js (Node) is an Open Source, cross-platform runtime environment for executing JavaScript code. Node is used extensively for server-side programming, making it possible for developers to use JavaScript for client-side and server-side code without needing to learn an additional language.

How does NodeJS Worker work? ›

Instead, the worker threads approach allows applications to use several isolated JavaScript workers, with Node providing communication between workers and the parent worker. Each worker in Node. js has its own V8 and Event Loop instance. Workers, unlike children's processes, can exchange memory.

What is the working structure of NodeJS? ›

Since Node. js uses an event-driven, non-blocking I/O model, it is used to create I/O-intensive web applications, including single-page applications, video streaming, online chatting apps, and other web applications. This structure allows Node. js developers to create efficient and lightweight web applications.

What is the best explanation of NodeJS? ›

Node.js is an open-source and cross-platform JavaScript runtime environment. It is a popular tool for almost any kind of project! Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. This allows Node.js to be very performant.

How does a node work? ›

Nodes Add Transactions To Blocks and Broadcast Them To The Network. Once the transaction moves to a pending status, miner or validator nodes are able to add the block to the network. At the point a miner or validator wins the block and adds it to the chain, the transaction becomes immutable.

How does Node.js work internally? ›

Node JS Web Server internally maintains a Limited Thread pool to provide services to the Client Requests. Node JS Web Server receives those requests and places them into a Queue. It is known as “Event Queue”. Node JS Web Server internally has a Component, known as “Event Loop”.

What is the work flow of NodeJS? ›

Parts of 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.

How does NodeJS work in frontend? ›

Node. js is a javascript runtime that uses the Chrome V8 javascript engine. The front-end already uses a javascript engine on the browser (V8 for Chrome, SpiderMonkey for Firefox, Chakra for Edge), so whether Javascript is running in the browser on in Node. js, you can expect very similar environments.

How does NodeJS work behind the scenes? ›

Node. js relies on a few critical libraries to work properly, notably the V8 JavaScript engine and Libuv within its runtime environment. V8 engine is what converts JavaScript code into machine code that computer can actually understand. Libuv is an open source library with a strong focus on asynchronous I/O.

What is the principle of NodeJS? ›

Asynchronous and Event-Driven: Node. js operates on a non-blocking, event-driven model, meaning it can handle multiple connections simultaneously without waiting for tasks to be completed. This approach enhances performance and scalability in applications, especially I/O-bound tasks.

What is process in NodeJS? ›

The process object in Node. js is a global object that can be accessed inside any module without requiring it. There are very few global objects or properties provided in Node. js and process is one of them.

How to understand NodeJS? ›

How to Start Learning Node. js
  1. Learn JavaScript. ...
  2. Understand Why It Is Called Node. ...
  3. Understand non-blocking in Node. ...
  4. Learn the Concept of the Event Loop. ...
  5. Learn the Global Variables. ...
  6. Learn How to Use the Libraries That Come With Node. ...
  7. Learn Code Writing for Node. ...
  8. Without Using Any Frameworks, Write a Web Application on Node.
Aug 13, 2024

What is NodeJS for dummies? ›

Node. js is an open source cross-platform runtime environment written in JavaScript. It is built on Chrome's V8 JavaScript engine, which parses and executes the JavaScript code. Node uses an event-driven, non-blocking I/O model, which makes it fast and lightweight.

How does node JS work in frontend? ›

Node. js is a javascript runtime that uses the Chrome V8 javascript engine. The front-end already uses a javascript engine on the browser (V8 for Chrome, SpiderMonkey for Firefox, Chakra for Edge), so whether Javascript is running in the browser on in Node. js, you can expect very similar environments.

Top Articles
These are the credit cards with the most satisfied customers
7 top fixed income investment options compared: Find out which suits you now
Po Box 7250 Sioux Falls Sd
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Tesla Supercharger La Crosse Photos
Kokichi's Day At The Zoo
Kansas Craigslist Free Stuff
Shorthand: The Write Way to Speed Up Communication
Obituary (Binghamton Press & Sun-Bulletin): Tully Area Historical Society
Best Theia Builds (Talent | Skill Order | Pairing + Pets) In Call of Dragons - AllClash
Acbl Homeport
123 Movies Babylon
Mercy MyPay (Online Pay Stubs) / mercy-mypay-online-pay-stubs.pdf / PDF4PRO
Springfield Mo Craiglist
Love In The Air Ep 9 Eng Sub Dailymotion
Midlife Crisis F95Zone
065106619
Craftology East Peoria Il
Eva Mastromatteo Erie Pa
Palm Coast Permits Online
Bj Alex Mangabuddy
Best Nail Salons Open Near Me
What Is The Lineup For Nascar Race Today
Jordan Poyer Wiki
Prot Pally Wrath Pre Patch
Walmart Pharmacy Near Me Open
Beaufort 72 Hour
Bleacher Report Philadelphia Flyers
4Oxfun
JVID Rina sauce set1
Marokko houdt honderden mensen tegen die illegaal grens met Spaanse stad Ceuta wilden oversteken
Ou Football Brainiacs
Miles City Montana Craigslist
Hrconnect Kp Login
Angel Haynes Dropbox
Publix Christmas Dinner 2022
Mini-Mental State Examination (MMSE) – Strokengine
Motor Mounts
Kamzz Llc
4083519708
Second Chance Apartments, 2nd Chance Apartments Locators for Bad Credit
Kutty Movie Net
6576771660
30 Years Of Adonis Eng Sub
Port Huron Newspaper
Devotion Showtimes Near Showplace Icon At Valley Fair
Headlining Hip Hopper Crossword Clue
552 Bus Schedule To Atlantic City
Germany’s intensely private and immensely wealthy Reimann family
Roller Znen ZN50QT-E
Sam's Club Fountain Valley Gas Prices
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6366

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.