Chapter 1: Node.js Get Started
Node.js” guide — written as if I’m sitting next to you with a coffee, showing you everything on my screen step by step, explaining why we do each thing, and warning about the most common beginner traps.
Let’s really begin from zero — no skipping steps.
Step 0 – Mindset before we touch the keyboard
Most people get stuck in the first 2–3 days because of these wrong expectations:
- “I will write backend code like frontend React code” → no, structure and thinking are quite different
- “I need to learn everything at once (Express + Mongo + JWT + React + Docker…)” → biggest mistake
- “Node.js is very hard” → actually very easy to start, very hard to master well
Good first-week goal (2026):
- Run a server
- Understand folders & files
- Make GET + POST requests
- See your own data coming back
- Feel “I can actually build something real”
That’s what we’re going to do today — very cleanly.
Step 1 – Install Node.js the smart way (2026 recommendation)
Do NOT just click “download LTS” from nodejs.org if you plan to code seriously.
Best way = use a version manager → you can switch versions in 10 seconds later.
Windows / macOS / Linux – use nvm (most popular)
macOS & Linux
|
0 1 2 3 4 5 6 7 |
# Install nvm curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash |
Close terminal completely and open a new one, then:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
# Install and use latest LTS (very stable in 2026) nvm install 20 nvm use 20 # Optional: also install latest version to play with new features nvm install 22 |
Windows (recommended: nvm-windows)
https://github.com/coreybutler/nvm-windows/releases → download & install latest .exe
Then in PowerShell or Command Prompt:
|
0 1 2 3 4 5 6 7 |
nvm install 20 nvm use 20 |
Verify installation
|
0 1 2 3 4 5 6 7 |
node --version # should show v20.x.x npm --version # should show 10.x.x or higher |
You now have Node.js and npm (the package manager).
Step 2 – Create your first real project folder
|
0 1 2 3 4 5 6 7 |
mkdir my-first-node-api cd my-first-node-api |
Now initialize it properly:
|
0 1 2 3 4 5 6 |
npm init -y |
This creates package.json — think of it as the identity card of your project.
Open the folder in VS Code (or any editor you like):
|
0 1 2 3 4 5 6 |
code . |
Step 3 – Install only what we need right now
We’ll use Express — the most popular & beginner-friendly web framework for Node.js.
|
0 1 2 3 4 5 6 |
npm install express |
Also install a very helpful development tool:
|
0 1 2 3 4 5 6 |
npm install --save-dev nodemon |
nodemon = auto-restart server when you save a file → huge time saver.
Step 4 – First working server (modern style – 2026)
Create file: server.js
|
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 |
// server.js import express from 'express'; const app = express(); // Very important: let Express understand JSON bodies app.use(express.json()); // ─── Routes ──────────────────────────────────────────────── app.get('/', (req, res) => { res.json({ message: "Hello! Your first Node.js server is alive! 🎉", time: new Date().toLocaleString() }); }); app.get('/hello', (req, res) => { res.send(` <h1>Hello from Node.js</h1> <p>This is plain HTML response</p> <a href="/">Go back</a> `); }); // Start the server const PORT = 4000; app.listen(PORT, () => { console.log(`─────────────────────────────────────────────`); console.log(`🚀 Server is running`); console.log(` → http://localhost:${PORT}`); console.log(`─────────────────────────────────────────────`); }); |
Important change for modern Node.js
Open package.json and add this line:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
{ "name": "my-first-node-api", ... "type": "module", // ← ADD THIS LINE ... } |
This tells Node.js: “please use modern import syntax instead of old require”
Step 5 – Add start scripts (very important habit)
Update package.json → scripts section:
|
0 1 2 3 4 5 6 7 8 9 |
"scripts": { "start": "node server.js", "dev": "nodemon server.js" } |
Now you can run:
|
0 1 2 3 4 5 6 |
npm run dev |
You should see:
|
0 1 2 3 4 5 6 7 8 9 10 |
[nodemon] starting `node server.js` ───────────────────────────────────────────── 🚀 Server is running → http://localhost:4000 ───────────────────────────────────────────── |
Open browser: http://localhost:4000
You did it! 🎉
Step 6 – Add your first POST endpoint (real API feeling)
Add this code before the app.listen(…) line:
|
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 |
// POST /users – create a new user (fake for now) app.post('/users', (req, res) => { const user = req.body; // ← this is the JSON you send from client // Very simple validation if (!user.name || !user.email) { return res.status(400).json({ error: "Name and email are required" }); } // Fake database save const newUser = { id: Math.floor(Math.random() * 10000), ...user, createdAt: new Date().toISOString() }; res.status(201).json({ message: "User created successfully", user: newUser }); }); |
Now test it (you can use browser extensions, Postman, or terminal):
Using curl (terminal)
|
0 1 2 3 4 5 6 7 8 |
curl -X POST http://localhost:4000/users \ -H "Content-Type: application/json" \ -d '{"name": "Aman", "email": "aman@example.com", "age": 25}' |
You should get back something like:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{ "message": "User created successfully", "user": { "id": 7421, "name": "Aman", "email": "aman@example.com", "age": 25, "createdAt": "2026-02-07T12:34:56.789Z" } } |
Congratulations — you just built your first real API endpoint!
Step 7 – Summary table – What you already have
| File / Command | Purpose | Why important? |
|---|---|---|
| package.json | Project identity & scripts | Heart of every Node.js project |
| “type”: “module” | Allows modern import syntax | Future-proof, cleaner code |
| npm run dev | Auto-restart with nodemon | Saves hours of frustration |
| express.json() middleware | Parses incoming JSON | Lets you read req.body easily |
| res.json() | Sends JSON response | Most common way to answer API requests |
| res.status(201) | Sets proper HTTP status code | Good API practice |
| GET / | Home / health check | Always good to have |
| POST /users | Create resource | First taste of real backend work |
Step 8 – Next 3–5 days recommended path (very realistic)
Day 1 (today): → What you just did — basic server + GET + POST
Day 2: → Query parameters (?id=123&sort=desc) → Route parameters (/users/:id) → Basic input validation (if/else or later Zod)
Day 3: → Separate routes into folders → Create routes/users.js → Learn router from Express
Day 4: → Connect to a real database (MongoDB Atlas free or PostgreSQL local)
Day 5: → Learn how to handle errors globally → Add simple .env file + dotenv
Which direction would you like to go next?
Just tell me one thing you want to do right now:
- Add query parameters and route parameters with examples
- Move routes to separate files (cleaner structure)
- Validate data properly (using Zod – very modern & clean)
- Connect to MongoDB (easiest database for beginners)
- Handle errors in a professional way
- Use .env file for secrets & configuration
- Create GET /users and fake in-memory database
- Understand what middleware really is with examples
Pick one number (or write what you want) — I’ll continue exactly from your current code with very detailed steps.
Happy coding — you’re already further than most beginners after one session! 🚀
