Working of Nodejs

Lesson 2 – How Nodejs works


Welcome to the second installment of our Node.js journey! In this lesson, we will unravel the inner workings of Node.js, focusing on the core concepts that make it a powerhouse for developing efficient and scalable applications.


 2.1 Event Loop and Non-Blocking I/O


 2.1.1 Event Loop: The Heartbeat of Node.js


Event-Driven Architecture:

- Node.js operates on an event-driven, non-blocking architecture.

- Events are fundamental occurrences in a Node.js application, triggering specific actions.


Understanding the Event Loop:

- The event loop is the central mechanism that enables Node.js to handle multiple operations simultaneously.

- It constantly listens for events, executes associated callbacks, and manages asynchronous tasks.




// Simple example of an event loop in Node.js

const fs = require('fs');


fs.readFile('example.txt', 'utf8', (err, data) => {

    if (err) throw err;




console.log('Reading file asynchronously...');




- The `readFile` function initiates an asynchronous file read operation.

- While the file is being read, the event loop allows the program to continue with the next statement (`console.log`).

- Once the file read operation is complete, the callback function is executed.


 2.1.2 Non-Blocking I/O: Unleashing Efficiency


Overview of Non-Blocking I/O:

- Non-blocking I/O ensures that the execution of your program is not halted while waiting for I/O operations to complete.

- It allows handling multiple concurrent operations without waiting for each one to finish.


Comparison with Traditional Blocking I/O:

- Traditional blocking I/O operations would halt the entire program until a task completes.

- Non-blocking I/O enhances the application's responsiveness and scalability.




// Blocking I/O

const fs = require('fs');


const data = fs.readFileSync('example.txt', 'utf8');



console.log('Reading file synchronously...');




- In the synchronous read operation (`readFileSync`), the program waits until the file is completely read before moving to the next statement.

- This approach can lead to performance bottlenecks in applications with heavy I/O operations.


 2.2 Nodejs Modules


 2.2.1 Creating and Importing Modules


Defining Modules in Node.js:

- Modules are an integral part of Node.js, allowing code organization and reusability.

- Each file in Node.js is treated as a module.


Importing and Using Modules:

- The `require` function is used to import modules into your code.

- Encapsulation of functionality within modules promotes cleaner and maintainable code.




// Module definition in 'myModule.js'

const greeting = () => {

    console.log('Hello from myModule!');



module.exports = greeting;




// Importing and using the module in 'main.js'

const myModule = require('./myModule');


myModule(); // Output: Hello from myModule!




- The `greeting` function in `myModule.js` is encapsulated within the module.

- In `main.js`, we import the module using `require` and call the `greeting` function.


 2.2.2 npm Packages


Exploring npm Packages:

- npm (Node Package Manager) is a repository of packages and modules for Node.js.

- Thousands of open-source packages are available to enhance the functionality of your applications.


Installing and Managing External Packages using npm:

- Use the `npm install` command to install external packages.

- The `package.json` file keeps track of project dependencies.




 Installing a package using npm

npm install express




// Extract from 'package.json' after installing Express.js


  "dependencies": {

    "express": "^4.17.1"






- The `npm install express` command installs the Express.js framework.

- The installed package and its version are added to the `dependencies` section in `package.json`.



Armed with a deeper understanding of Node.js's event-driven architecture, non-blocking I/O, and modularization, you're now better equipped to build scalable and efficient applications. In the upcoming modules, we'll delve even further into the intricacies of Node.js, unlocking its full potential for your development endeavors.