1. What is Node.js really? (in plain words)
Node.js = JavaScript running outside the browser
- It lets you use JavaScript to write server-side code
- It lets you build backend APIs, CLIs, scripts, microservices, real-time apps, etc.
- Very fast at I/O heavy tasks (files, network, databases, APIs…)
Most popular use case in 2025–2026: Building REST APIs + GraphQL APIs + WebSocket servers
2. Installation (do it properly)
Option A: Recommended (2025–2026 way) → Use nvm (Node Version Manager)
Windows / macOS / Linux:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
# macOS & Linux curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash # Close & reopen terminal, then: nvm install 20 # LTS (very stable in 2026) nvm install 22 # Current (newest features) nvm use 20 nvm alias default 20 |
|
0 1 2 3 4 5 6 7 8 |
# Check versions node -v npm -v |
You should see something like:
|
0 1 2 3 4 5 6 7 |
v20.17.0 10.8.3 |
(Or newer — that’s fine)
Option B: Quick install (not recommended long-term)
Just download from https://nodejs.org
Choose LTS version.
3. Hello World – 3 different styles
Style 1: Most basic
Create file hello.js
|
0 1 2 3 4 5 6 |
console.log("Hello from Node.js! 🚀"); |
Run:
|
0 1 2 3 4 5 6 |
node hello.js |
Style 2: Tiny HTTP server (classic Node.js hello world)
server.js
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const http = require("node:http"); const server = http.createServer((req, res) => { res.writeHead(200, { "Content-Type": "text/plain" }); res.end("Hello World from pure Node.js! 🌍\n"); }); server.listen(3000, () => { console.log("Server running → http://localhost:3000"); }); |
|
0 1 2 3 4 5 6 |
node server.js |
Open browser → http://localhost:3000
Style 3: Modern ESM style (2025+ best practice)
server.mjs
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import { createServer } from "node:http"; createServer((req, res) => { res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify({ message: "Hello from modern Node.js!", time: new Date().toISOString() })); }).listen(4000, () => { console.log("ESM server → http://localhost:4000"); }); |
Run:
|
0 1 2 3 4 5 6 |
node server.mjs |
4. Important Project Structure (real-world style)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
my-api/ ├── src/ │ ├── index.js ← entry point │ ├── config/ ← env, database config │ ├── controllers/ ← request handlers │ ├── routes/ ← route definitions │ ├── middleware/ ← auth, validation, error handling │ ├── services/ ← business logic │ ├── models/ ← database models (if using ORM) │ └── utils/ ← helpers, logger, etc. ├── tests/ ← unit + integration tests ├── .env ├── .env.example ├── package.json └── README.md |
5. package.json – the heart of every Node.js project
Most important fields in 2025–2026:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
{ "name": "my-cool-api", "version": "1.0.0", "type": "module", // ← very important! "main": "src/index.js", "scripts": { "start": "node src/index.js", "dev": "nodemon src/index.js", "lint": "eslint .", "test": "jest --coverage", "format": "prettier --write ." }, "dependencies": { "express": "^4.19.2", "dotenv": "^16.4.5", "zod": "^3.23.8" }, "devDependencies": { "nodemon": "^3.1.7", "eslint": "^9.9.1", "prettier": "^3.3.3", "jest": "^29.7.0" } } |
6. Modern Express.js API – Full Example (2025–2026 style)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
// src/index.js import express from 'express'; import dotenv from 'dotenv'; import { z } from 'zod'; dotenv.config(); const app = express(); const port = process.env.PORT || 5000; app.use(express.json()); // Input validation schema const createUserSchema = z.object({ name: z.string().min(2).max(50), email: z.string().email(), age: z.number().int().min(18).max(120).optional() }); // Routes app.get('/health', (req, res) => { res.json({ status: 'ok', uptime: process.uptime() }); }); app.post('/users', (req, res) => { try { const data = createUserSchema.parse(req.body); // In real app → save to database here res.status(201).json({ message: 'User created', user: data }); } catch (err) { res.status(400).json({ error: 'Validation failed', issues: err.errors }); } }); // 404 handler app.use((req, res) => { res.status(404).json({ error: 'Not found' }); }); // Global error handler app.use((err, req, res, next) => { console.error(err.stack); res.status(500).json({ error: 'Something broke!' }); }); app.listen(port, () => { console.log(`🚀 Server running on http://localhost:${port}`); console.log(`Environment: ${process.env.NODE_ENV || 'development'}`); }); |
Run it:
|
0 1 2 3 4 5 6 |
npm run dev |
Try:
|
0 1 2 3 4 5 6 7 8 |
curl -X POST http://localhost:5000/users \ -H "Content-Type: application/json" \ -d '{"name":"Aman","email":"aman@example.com","age":25}' |
7. Most Important Node.js Concepts You Must Understand
| Topic | Why it matters | Quick Tip / Gotcha |
|---|---|---|
| Event Loop | Heart of Node.js | Never block it with heavy computation |
| require vs import | CommonJS vs ESM | Use ESM (import) in 2025+ |
| process.env | Environment variables | Always use dotenv |
| async/await | 90% of real code | Handle errors properly! |
| Streams | Files > 100MB, video, logs | Use pipeline() from node:stream/promises |
| Clustering | Use all CPU cores | cluster module or pm2 |
| Error handling | Crashes kill production | Use domains + global handler |
| npm vs pnpm vs bun | Package manager speed & disk usage | pnpm or bun → much better in 2026 |
8. Recommended Modern Tech Stack (2026)
Very popular & production-ready combination:
- Runtime: Node.js 20 LTS or 22
- Framework: Express (classic) or Fastify (faster) or Hono (very modern & lightweight)
- Validation: Zod
- ORM/Database: Prisma + PostgreSQL / Drizzle + PostgreSQL
- Auth: JWT + cookie-session or ** Lucia** / NextAuth/Auth.js
- Testing: Jest + Supertest or Vitest
- Linting/Formatting: ESLint flat config + Prettier
- Package manager: pnpm or bun
- Deployment: Docker + Render / Railway / Fly.io / Vercel (for serverless)
Want to go deeper in any of these areas?
- Async patterns & common mistakes
- File upload (very common question)
- Authentication & authorization (JWT vs sessions)
- WebSockets (real-time chat)
- How to structure big Node.js projects
- Performance tips & benchmarking
- Deploying Node.js properly in 2026
Just tell me which topic you want next — I’ll go as deep as you want! 😄
