Node.js is an open-source, cross-platform, JavaScript runtime environment that allows you to run JavaScript outside the browser.

βœ… Traditionally, JavaScript was used only in browsers (for front-end).
βœ… With Node.js, you can use JavaScript on the server-side to build scalable and fast network applications.

🧩 Why Use Node.js?

Here’s why Node.js became so popular:

  1. Uses JavaScript Everywhere – One language for both front-end and back-end.
  2. Fast Execution – Built on Google’s V8 JavaScript engine (used in Chrome).
  3. Asynchronous & Non-blocking I/O – Handles multiple requests efficiently.
  4. Event-driven architecture – Great for real-time applications (like chat apps).
  5. Huge npm Ecosystem – Access to thousands of open-source packages.
βš™οΈ How Node.js Works

Node.js runs on the V8 engine, which converts JavaScript into machine code.
It uses an event loop to handle many connections simultaneously without creating multiple threads.

πŸ” Event Loop Concept:

  • Node.js is single-threaded.
  • When a task (like reading a file or database query) takes time, Node.js delegates it and continues executing other code.
  • Once the task finishes, a callback is triggered.
πŸ’» Installing Node.js
  1. Download from: https://nodejs.org
  2. Verify installation: node -v npm -v
  3. Create a new file: touch app.js
  4. Run your file: node app.js
🧠 Basic Example
// app.js
console.log("Hello from Node.js!");

Run:

node app.js

Output:

Hello from Node.js!
πŸ“¦ npm – Node Package Manager

npm is the world’s largest package library.
It comes bundled with Node.js and lets you install packages easily.

Example:

npm install express
  • Installs the Express.js web framework.
  • Packages are stored in the node_modules folder.

To initialize a Node project:

npm init -y

This creates a package.json file to track dependencies and metadata.

🧱 Core Modules in Node.js

Node.js provides built-in modules β€” no need to install them.

ModuleDescription
fsFile System operations
httpCreate web servers
pathHandle file paths
osInformation about operating system
urlURL utilities
eventsWork with event-driven programming

Example – Using fs module:

const fs = require('fs');

// Write to a file
fs.writeFileSync('hello.txt', 'Welcome to Node.js!');

// Read the file
const data = fs.readFileSync('hello.txt', 'utf8');
console.log(data);
🌐 Building a Simple Server

Let’s create a basic HTTP server using Node’s built-in http module:

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, this is a Node.js server!');
});

server.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

Run:

node app.js

Then open your browser at http://localhost:3000.

⚑ Asynchronous Programming

Node.js excels at non-blocking, asynchronous code.

Example:

const fs = require('fs');

console.log("Start reading...");

fs.readFile('hello.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

console.log("End of program");

πŸ‘‰ Output order:

Start reading...
End of program
Welcome to Node.js!

Because file reading happens asynchronously!

πŸ”₯ Frameworks Built on Node.js
FrameworkPurpose
Express.jsSimplifies server creation and routing
NestJSEnterprise-level framework using TypeScript
Next.jsFull-stack React framework (runs on Node)
Socket.ioReal-time communication (e.g., chat apps)
ElectronBuild desktop apps using JS, HTML, and CSS
πŸš€ Summary
ConceptDescription
TypeJavaScript runtime environment
Created ByRyan Dahl (2009)
Built OnGoogle Chrome V8 engine
ArchitectureEvent-driven, non-blocking I/O
Language UsedJavaScript
Ideal ForAPIs, web servers, real-time apps
βœ… Example Use Cases of Node.js
  • Building REST APIs
  • Real-time chat applications
  • Streaming services (like Netflix)
  • Command-line tools
  • Backend for single-page apps

2 Comments

  1. shivam dunni

    Really great content! The examples you provided were so helpful, and I feel a lot more confident with this now. Keep up the amazing work

  2. Kavitha

    Highlight the impact: Explain how their notes specifically helped you.
    For example, Your summary on the last chapter really helped me grasp the main idea.

Leave a Reply

Your email address will not be published. Required fields are marked *