Chapter 46: Node.js Express.js

Express.js in Node.js.

I will explain it as if we are sitting together right now:

  • I open VS Code
  • I create files one by one
  • I explain why we are doing each step
  • I show the exact commands I type in the terminal
  • I highlight the most common beginner traps and intermediate mistakes
  • I share real production patterns used in 2025–2026 by serious teams
  • We build a small but realistic REST API together from zero

Let’s start from scratch — no skipping.

Step 1 – Realistic project initialization (what most people actually do)

Bash

Why these packages?

  • cors — almost every API needs it
  • helmet — basic security headers (very quick win)
  • compression — gzip responses (reduces bandwidth 60–80%)
  • dotenv — environment variables
  • zod — runtime validation (very popular replacement for Joi)
  • tsx — run TypeScript directly (no build step in dev)
  • nodemon — auto-restart on file change

Step 2 – Realistic tsconfig.json (strict but practical)

JSON

Why these settings?

  • “module”: “NodeNext” + “moduleResolution”: “NodeNext” → best ESM support
  • “strict”: true + “noImplicitAny”: true → catches many bugs early
  • “esModuleInterop”: true → makes importing CommonJS modules nicer

Step 3 – Realistic folder structure (what most teams use)

text

Why this structure?

  • Controllers are thin → they just call services & handle HTTP
  • Services contain real business logic (easy to test)
  • Schemas are separate → reusable for validation & OpenAPI
  • Middleware is reusable (auth, rate-limit, error, etc.)
  • Very easy to grow into microservices or feature folders later

Step 4 – Environment variables + Zod validation

src/config/env.ts

TypeScript

Why Zod here?

  • Compile-time + runtime safety
  • Beautiful error messages when .env is wrong
  • Automatic type inference (env.PORT is number)

Step 5 – Basic Express server with TypeScript

src/index.ts

TypeScript

Step 6 – Custom error handling (production essential)

src/middleware/error.middleware.ts

TypeScript

Why this pattern?

  • Distinguishes expected errors (404, 401, validation) from bugs
  • Never leaks stack traces in production
  • Consistent JSON error shape for frontend

Step 7 – Input validation with Zod (very modern & strongly recommended)

src/schemas/task.schema.ts

TypeScript

src/middleware/validate.middleware.ts

TypeScript

Usage in route

TypeScript

Step 8 – Realistic controller example

src/controllers/task.controller.ts

TypeScript

Summary – Modern Node.js + Express in 2025–2026 feels like this

You now have:

  • ESM + top-level await
  • Strict TypeScript
  • Zod runtime validation
  • Custom AppError + global error handler
  • Clean layered architecture
  • Automatic formatting & linting
  • Security headers & compression
  • Environment validation

This is the foundation used by most serious Express-based Node.js backends today.

Which direction would you like to go next?

  • Add JWT authentication + protected routes
  • Connect Prisma or Drizzle with full typing
  • Implement rate limiting, logging (pino), request tracing
  • Add unit & integration tests with Vitest
  • Docker + production deployment checklist
  • Migrate this project to Fastify or Hono (side-by-side comparison)

Just tell me what you want to build or understand next — I’ll continue with complete, realistic code. 😊

You may also like...

Leave a Reply

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