What, Why, How, When explained clearly for learners
Node.js is an open-source, cross-platform runtime that allows you to run JavaScript **outside the browser**, usually on the **server-side**. It uses the **V8 engine** (from Chrome) to execute JS code very fast.
It's not a programming language or framework β itβs a runtime that lets JavaScript work as a **backend language**.
Node.js uses the V8 engine to run JavaScript, and a powerful internal system called the event loop to handle tasks asynchronously.
Instead of creating multiple threads (like Java), it uses a **single-threaded model** with **non-blocking I/O**. This makes it lightweight and highly efficient.
Simple Code Example:
// hello.js
console.log("Hello from Node.js!");
Run it in terminal using:
node hello.js
NPM (Node Package Manager) comes with Node.js. It helps you install and manage **third-party libraries** (called packages).
Example:
npm install express
This installs the popular web framework Express.js.
fs β File System (read/write/delete files)http β Create basic HTTP serverpath β Work with file pathsexpress β Framework for building REST APIsdotenv β Load environment variables
// api.js
const http = require('http');
const server = http.createServer((req, res) => {
res.end('Hello from backend!');
});
server.listen(3000);
Run it with node api.js and open http://localhost:3000 in your browser.