Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • Software and Tools
    • School Learning
    • Practice Coding Problems
  • Go Premium
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Node.js Tutorial
Next article icon

How Node.js Works?

Last Updated : 25 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Node.js is a powerful, event-driven runtime environment that allows you to run JavaScript code outside the browser, primarily on the server side. Unlike traditional server-side technologies like PHP and Django (Python), Node.js offers non-blocking, asynchronous processing, making it highly efficient and scalable for real-time applications. Its lightweight architecture and single-threaded event loop give it a performance edge in handling concurrent requests.

The Birth of Node.js: Taking JavaScript Beyond the Browser

JavaScript is a scripting language that usually runs inside web browsers. Each browser has its own JavaScript engine to run the code—for example, Chrome uses V8, Firefox uses SpiderMonkey, and Safari uses Apple’s JavaScript engine. That’s why JavaScript can only run inside the browser.

But Ryan Dahl, a software engineer, created something called Node.js. He took the V8 engine and combined it with C++ so that JavaScript could run outside the browser. This made it possible to do things like file handling with JavaScript, which wasn’t possible before.

Note: Node.js is not a library or framework. It’s a JavaScript Runtime Environment.

The Benefits of Node.js are as follows:

  • You can run JavaScript outside the browser.
  • JavaScript can now interact with the computer's system.
  • You can create web servers using JavaScript.

Npm in Node.JS

NPM stands for Node Package Manager. When we install Node.js, it automatically installs NPM. When we are making any new project, we have to make a command in the terminal, such as

npm init // init stands for initialization

It will create a package.json File. It records important metadata about a project, which is required before publishing to NPM, and also defines functional attributes of a project that NPM uses to install dependencies, run scripts, and identify the entry point to our package.

How Node.js Handles Client Requests (Based on Event Loop & Thread Pool)

In a Node.js environment, handling client requests efficiently is Important for building fast and scalable web applications. When a client interacts with a web application, it sends a request to the web server. This request can either be blocking (synchronous) or non-blocking (asynchronous). Node.js uses an Event-Driven Architecture where all incoming requests are first placed into an Event Queue. An Event Loop continuously monitors this queue and processes each request accordingly. Understanding how Node.js handles these requests using the Event Queue, Event Loop, and Thread Pool is essential to optimizing performance and avoiding delays caused by blocking operations.


Follow Step-by-Step to send Request to Node.js Server

  • The client sends a request to the Node.js web server to interact with the web application.
  • The request can be either blocking (synchronous) or non-blocking (asynchronous). Node.js receives the request and adds it to the Event Queue. Requests from different users (User 1, 2, 3, etc.) are added to the Event Queue.
  • Requests are picked in (FIFO – First In, First Out) order. If the request is non-blocking, it is processed immediately, and the response is sent back to the client. If the request is a blocking operation, it is passed to the Thread Pool.
  • The Thread Pool has a limited number of threads. If a thread is free, the blocking task is assigned to it.
  • Once the task is completed, the thread returns the result back to the Event Loop, which then sends the response to the client.
  • If all threads are busy, any new blocking requests must wait in a queue until a thread becomes free.
  • This waiting increases the response time for the client. Therefore, it is always better to avoid blocking operations and use non-blocking alternatives whenever possible to maintain performance and scalability.

Note: A Thread is a worker who is working for us. Thread is responsible for working on our Blocking Request.

Blocking or Synchronous Operation

In a blocking or synchronous operation, tasks are executed one at a time in a specific sequence. The program waits for the current task to complete before moving on to the next one. This means if a task takes time—like reading a file or making a network request—the entire program waits until that task finishes. While this approach is simple and easy to understand, it can slow down performance, especially when handling multiple requests or time-consuming operations.

Here's an example to understand synchronous operations:

The script uses fs.readFileSync() which blocks the execution which blocks the execution until the file (sync.txt) is completely read.It reads the file content in UTF-8 encoding and prints it using console.log(result);.

Blocking
Blocking or synchronous


This shows the output of the synchronous file read from sync.txt. The content eisha: +91 1111111111 and ruhi : +91 2222222222 is printed, which means the file read was completed before moving forward.

synchronous file


Additional logs are added ("1", "2", "3") to demonstrate the blocking nature of fs.readFileSync. Code will pause at fs.readFileSync until the file is read

blocking nature


This output demonstrates synchronous behaviour . First "1" is printed , Then File Content is printed , Then "2" and "3" is printed after the file is read.

synchronous
Synchronous Behaviour

Non-Blocking or Asynchronous Operation

In a non-blocking or asynchronous operation, tasks are executed without waiting for the previous one to finish. Instead of pausing the program, long-running operations like file reading or API calls are handled in the background. Once the task is complete, a callback or promise is used to handle the result. This approach allows the program to stay responsive and handle multiple tasks at the same time, making it ideal for high-performance and real-time applications like web servers.

Non-Blocking Request
Non-Blocking Request

In the above output of Non-Blocking or Asynchronous operation we can see that sequence of execution is Asynchronous as 2 and 3 executed before result can be executed . it means Asynchronous operation Cannot blocked the execution of other till the readFileExecution can take place.

Conclusion

Node.js has extended the power of JavaScript beyond the browser by allowing it to run on the server side using the V8 engine and C++. With features like the Event Loop, Event Queue, and Thread Pool, Node.js efficiently handles both blocking and non-blocking operations, making it ideal for building fast and scalable web applications. By using non-blocking (asynchronous) operations, developers can avoid performance slowdown and ensure smooth execution. Combined with tools like NPM for managing dependencies, Node.js has become a powerful runtime environment for modern full-stack development.


Next Article
Node.js Tutorial

M

mahimabh9a46
Improve
Article Tags :
  • Node.js
  • Node.js
  • nodejs

Similar Reads

    How Node.js works behind the scene ?
    Node.js is the JavaScript runtime environment which is based on Google’s V8 Engine i.e. with the help of Node.js we can run the JavaScript outside of the browser. Other things that you may or may not have read about Node.js is that it is single-threaded, based on event-driven architecture, and non-b
    5 min read
    Node.js Tutorial
    Node.js is a powerful, open-source, and cross-platform JavaScript runtime environment built on Chrome's V8 engine. It allows you to run JavaScript code outside the browser, making it ideal for building scalable server-side and networking applications.JavaScript was mainly used for frontend developme
    4 min read
    Node.js Tutorial
    Node.js is a powerful, open-source, and cross-platform JavaScript runtime environment built on Chrome's V8 engine. It allows you to run JavaScript code outside the browser, making it ideal for building scalable server-side and networking applications.JavaScript was mainly used for frontend developme
    4 min read
    NodeJS Basics
    NodeJS is a JavaScript runtime environment built on Chrome’s V8 JavaScript engine that allows developers to execute JavaScript code outside the browser. It can make console-based and web-based NodeJS applications. Some of the features of the NodeJs are mentioned below:Non-blocking I/O: NodeJS is asy
    5 min read
    NodeJS Basics
    NodeJS is a JavaScript runtime environment built on Chrome’s V8 JavaScript engine that allows developers to execute JavaScript code outside the browser. It can make console-based and web-based NodeJS applications. Some of the features of the NodeJs are mentioned below:Non-blocking I/O: NodeJS is asy
    5 min read
    NodeJS Basics
    NodeJS is a JavaScript runtime environment built on Chrome’s V8 JavaScript engine that allows developers to execute JavaScript code outside the browser. It can make console-based and web-based NodeJS applications. Some of the features of the NodeJs are mentioned below:Non-blocking I/O: NodeJS is asy
    5 min read
`; $(commentSectionTemplate).insertBefore(".article--recommended"); } loadComments(); }); }); function loadComments() { if ($("iframe[id*='discuss-iframe']").length top_of_element && top_of_screen articleRecommendedTop && top_of_screen articleRecommendedBottom)) { if (!isfollowingApiCall) { isfollowingApiCall = true; setTimeout(function(){ if (loginData && loginData.isLoggedIn) { if (loginData.userName !== $('#followAuthor').val()) { is_following(); } else { $('.profileCard-profile-picture').css('background-color', '#E7E7E7'); } } else { $('.follow-btn').removeClass('hideIt'); } }, 3000); } } }); } $(".accordion-header").click(function() { var arrowIcon = $(this).find('.bottom-arrow-icon'); arrowIcon.toggleClass('rotate180'); }); }); window.isReportArticle = false; function report_article(){ if (!loginData || !loginData.isLoggedIn) { const loginModalButton = $('.login-modal-btn') if (loginModalButton.length) { loginModalButton.click(); } return; } if(!window.isReportArticle){ //to add loader $('.report-loader').addClass('spinner'); jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', { PRACTICE_API_URL: practiceAPIURL, PRACTICE_URL:practiceURL },function(responseTxt, statusTxt, xhr){ if(statusTxt == "error"){ alert("Error: " + xhr.status + ": " + xhr.statusText); } }); }else{ window.scrollTo({ top: 0, behavior: 'smooth' }); $("#report_modal_content").show(); } } function closeShareModal() { const shareOption = document.querySelector('[data-gfg-action="share-article"]'); shareOption.classList.remove("hover_share_menu"); let shareModal = document.querySelector(".hover__share-modal-container"); shareModal && shareModal.remove(); } function openShareModal() { closeShareModal(); // Remove existing modal if any let shareModal = document.querySelector(".three_dot_dropdown_share"); shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" })); document.querySelector(".hover__share-modal-container").append( Object.assign(document.createElement('div'), { className: "share__modal" }), ); document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" })); const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"]; socialOptions.forEach((socialOption) => { const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" }); const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` }); const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` }); const shareLink = (socialOption === "Copy Link") ? Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) : Object.assign(document.createElement('a'), { className: "link-container" }); if (socialOption === "LinkedIn") { shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`); shareLink.setAttribute('target', '_blank'); } if (socialOption === "WhatsApp") { shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } if (socialOption === "Twitter") { shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } shareLink.append(icon, socialText); socialContainer.append(shareLink); document.querySelector(".share__modal").appendChild(socialContainer); //adding copy url functionality if(socialOption === "Copy Link") { shareLink.addEventListener("click", function() { var tempInput = document.createElement("input"); tempInput.value = window.location.href; document.body.appendChild(tempInput); tempInput.select(); tempInput.setSelectionRange(0, 99999); // For mobile devices document.execCommand('copy'); document.body.removeChild(tempInput); this.querySelector(".share__option-text").textContent = "Copied" }) } }); // document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu")); } function toggleLikeElementVisibility(selector, show) { document.querySelector(`.${selector}`).style.display = show ? "block" : "none"; } function closeKebabMenu(){ document.getElementById("myDropdown").classList.toggle("show"); }
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences