Skip to content

A comprehensive, production-ready REST API demonstrating industry best practices for building scalable, maintainable server applications.

Notifications You must be signed in to change notification settings

codezelaca/se-node-server-rest-apis

Repository files navigation

Production-Ready Node.js REST API

A comprehensive, production-ready REST API demonstrating industry best practices for building scalable, maintainable server applications.

🎯 What This Project Teaches

This is a learning repository designed to teach you how to build production-ready REST APIs. Every concept is implemented with extensive documentation, code comments, and explanations.

You Will Learn

  1. βœ… Input Validation - How to validate and sanitize all client input
  2. βœ… Concurrency Control - How to prevent race conditions and overselling
  3. βœ… Idempotency - How to handle duplicate requests safely
  4. βœ… Caching - How to improve performance with intelligent caching
  5. βœ… Structured Logging - How to debug production issues with tracing
  6. βœ… Security - How to protect against common vulnerabilities
  7. βœ… Error Handling - How to return consistent, helpful error messages
  8. βœ… API Design - How to design clean, intuitive REST APIs
  9. βœ… TypeScript - How to write type-safe Node.js applications
  10. βœ… Testing - How to validate your code works correctly

πŸš€ Quick Start

Prerequisites

  • Node.js >= 20.0.0
  • npm >= 10.0.0
  • Git (for cloning)

Installation

# Clone the repository
git clone https://github.com/your-username/se-node-server-rest-apis.git
cd se-node-server-rest-apis

# Install dependencies
npm install

# Set up environment (optional - defaults work for development)
cp .env.example .env

# Run database migrations
npm run db:migrate

# Seed the database with sample data
npm run db:seed

# Start the development server
npm run dev

The API will be available at http://localhost:3000

Test It Out

# Check health
curl http://localhost:3000/health

# List items
curl http://localhost:3000/api/v1/items

# Reserve an item
curl -X POST http://localhost:3000/api/v1/reserve \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: test-key-123" \
  -d '{"userId":"user_test","itemId":"item_1","qty":1}'

πŸ“š Learning Path

Start Here: The Documentation

We've created comprehensive learning documentation in the docs/ directory:

  1. docs/01-introduction.md - Overview and architecture
  2. docs/02-validation.md - Input validation with Zod
  3. docs/03-concurrency.md - Atomic database operations
  4. docs/04-idempotency.md - Handling duplicate requests
  5. docs/05-caching.md - Performance with caching
  6. docs/06-logging.md - Observability and tracing
  7. docs/07-validation-guide.md - Complete testing guide

πŸ’‘ Start with docs/README.md for the full learning guide.

Testing & Validation

Postman Collection

Ready-to-use Postman collection for testing all features:

πŸ› οΈ The Stack

Component Technology Why?
Runtime Node.js 20+ Latest LTS, ES2022 support
Framework Express 4.x Battle-tested, huge ecosystem
Language TypeScript 5.x Type safety, better DX
Database SQLite (better-sqlite3) Embedded, fast, zero-config
Validation Zod Runtime + compile-time validation
Logging Pino Ultra-fast structured logging
Security Helmet, CORS Security headers, cross-origin

πŸ“ Project Structure

se-node-server-rest-apis/
β”œβ”€β”€ docs/                           # Learning documentation
β”‚   β”œβ”€β”€ README.md                   # Documentation index
β”‚   β”œβ”€β”€ 01-introduction.md          # Overview
β”‚   β”œβ”€β”€ 02-validation.md            # Input validation
β”‚   β”œβ”€β”€ 03-concurrency.md           # Race conditions
β”‚   β”œβ”€β”€ 04-idempotency.md           # Duplicate requests
β”‚   β”œβ”€β”€ 05-caching.md               # Performance
β”‚   β”œβ”€β”€ 06-logging.md               # Observability
β”‚   └── 07-validation-guide.md      # Complete testing guide
β”‚
β”œβ”€β”€ src/                            # Source code
β”‚   β”œβ”€β”€ cache/                      # Caching layer
β”‚   β”‚   β”œβ”€β”€ index.ts                # TTL cache with invalidation
β”‚   β”‚   └── README.md               # Module learning guide
β”‚   β”œβ”€β”€ config/                     # Configuration
β”‚   β”‚   β”œβ”€β”€ index.ts                # Environment variables
β”‚   β”‚   └── README.md               # Module learning guide
β”‚   β”œβ”€β”€ database/                   # Database layer
β”‚   β”‚   β”œβ”€β”€ index.ts                # SQLite connection
β”‚   β”‚   β”œβ”€β”€ migrate.ts              # Migration runner
β”‚   β”‚   β”œβ”€β”€ seed.ts                 # Data seeding
β”‚   β”‚   └── README.md               # Module learning guide
β”‚   β”œβ”€β”€ http/                       # Response utilities
β”‚   β”‚   β”œβ”€β”€ index.ts                # ok(), fail() helpers
β”‚   β”‚   └── README.md               # Module learning guide
β”‚   β”œβ”€β”€ idempotency/                # Idempotency handling
β”‚   β”‚   β”œβ”€β”€ index.ts                # Duplicate request prevention
β”‚   β”‚   └── README.md               # Module learning guide
β”‚   β”œβ”€β”€ middleware/                 # Express middleware
β”‚   β”‚   β”œβ”€β”€ rateLimit.ts            # Token bucket rate limiting
β”‚   β”‚   β”œβ”€β”€ security.ts             # Security headers, CORS
β”‚   β”‚   └── README.md               # Module learning guide
β”‚   β”œβ”€β”€ observability/              # Logging & metrics
β”‚   β”‚   β”œβ”€β”€ index.ts                # Pino structured logging
β”‚   β”‚   └── README.md               # Module learning guide
β”‚   β”œβ”€β”€ routes/                     # API endpoints
β”‚   β”‚   β”œβ”€β”€ index.ts                # Main routes
β”‚   β”‚   β”œβ”€β”€ health.ts               # Health checks
β”‚   β”‚   └── README.md               # Module learning guide
β”‚   β”œβ”€β”€ services/                   # Business logic
β”‚   β”‚   β”œβ”€β”€ reservations.ts         # Domain logic
β”‚   β”‚   └── README.md               # Module learning guide
β”‚   β”œβ”€β”€ types/                      # TypeScript types
β”‚   β”‚   β”œβ”€β”€ index.ts                # All type definitions
β”‚   β”‚   └── README.md               # Module learning guide
β”‚   β”œβ”€β”€ validation/                 # Zod schemas
β”‚   β”‚   β”œβ”€β”€ schemas.ts              # Request/response schemas
β”‚   β”‚   └── README.md               # Module learning guide
β”‚   └── server.ts                   # Application entry point
β”‚
β”œβ”€β”€ postman-collection.json         # Postman collection (import this!)
β”œβ”€β”€ postman-environment.json        # Postman environment (import this!)
β”œβ”€β”€ POSTMAN_GUIDE.md                # Complete Postman instructions
β”œβ”€β”€ LEARNING_CHECKLIST.md           # Track your progress
β”œβ”€β”€ POSTMAN_COLLECTION.md           # API testing with curl
β”‚
β”œβ”€β”€ .env.example                    # Environment template
β”œβ”€β”€ package.json                    # Dependencies
β”œβ”€β”€ tsconfig.json                   # TypeScript config
└── README.md                       # This file

πŸŽ“ Core Concepts

1. Validation

Problem: Clients send invalid or malicious data.

Solution: Validate all input at the API boundary using Zod schemas.

// src/validation/schemas.ts
export const reserveRequestSchema = z.object({
  userId: z.string().min(1),
  itemId: z.string().regex(/^item_\d+$/),
  qty: z.number().int().min(1).max(5)
});

Learn more: docs/02-validation.md


2. Concurrency Control

Problem: Two users reserve the last item simultaneously, both succeed (overselling).

Solution: Atomic database operations with conditional updates.

UPDATE items
SET availableQty = availableQty - ?
WHERE id = ? AND availableQty >= ?

Learn more: docs/03-concurrency.md


3. Idempotency

Problem: Network retries create duplicate reservations.

Solution: Store responses by unique key, replay on duplicates.

// First request: Process and store
// Second request: Return cached response

Learn more: docs/04-idempotency.md


4. Caching

Problem: Repeated database queries waste resources.

Solution: Cache responses with TTL, invalidate on changes.

const cached = getCache('items');
if (cached) return cached;

const items = fetchItems();
setCache('items', items, 30_000);

Learn more: docs/05-caching.md


5. Observability

Problem: Debugging production issues is hard without context.

Solution: Structured logs with request tracing.

{
  "level": "info",
  "requestId": "abc-123",
  "msg": "Item reserved",
  "userId": "user_1",
  "itemId": "item_1"
}

Learn more: docs/06-logging.md


πŸ“– API Documentation

Base URL

http://localhost:3000/api/v1

Endpoints

Method Endpoint Description
GET /items List all items
GET /items/:id Get single item
POST /reserve Reserve an item
POST /confirm Confirm reservation
POST /cancel Cancel reservation
GET /reservations/user/:userId List user's reservations
GET /health Health check

Response Format

All responses follow a consistent format:

Success:

{
  "ok": true,
  "data": { ... }
}

Error:

{
  "ok": false,
  "error": {
    "code": "ERROR_CODE",
    "message": "Description",
    "details": { ... },
    "requestId": "uuid"
  }
}

πŸ§ͺ Testing the API

Option 1: Postman (Recommended)

Complete testing setup ready to use:

  1. Import postman-collection.json into Postman
  2. Import postman-environment.json into Postman
  3. Select "Reservation API - Local" environment
  4. Start testing!

πŸ“– Full instructions: POSTMAN_GUIDE.md

Option 2: cURL / Terminal

# Health check
curl http://localhost:3000/health

# List items
curl http://localhost:3000/api/v1/items

# Reserve an item
curl -X POST http://localhost:3000/api/v1/reserve \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: test-key-123" \
  -d '{"userId":"user_test","itemId":"item_1","qty":1}'

# Confirm reservation
curl -X POST http://localhost:3000/api/v1/confirm \
  -H "Content-Type: application/json" \
  -d '{"userId":"user_test","reservationId":"res_abc123"}'

πŸ“– More examples: POSTMAN_COLLECTION.md

Option 3: Learning Checklist

Track your learning progress and validate each feature:

πŸ“‹ Checklist: LEARNING_CHECKLIST.md

πŸ› οΈ Development

Available Scripts

npm run dev          # Start development server with hot reload
npm run build        # Compile TypeScript
npm run start        # Start production server
npm run lint         # Lint code
npm run format       # Format code
npm run db:migrate   # Run database migrations
npm run db:seed      # Seed database
npm run clean        # Clean build output

Environment Variables

# .env file (copy from .env.example)
NODE_ENV=development
PORT=3000
LOG_LEVEL=debug
DB_PATH=./app.db

πŸ“¦ Project Features

βœ… Implemented

  • Input Validation with Zod schemas
  • Atomic Operations to prevent race conditions
  • Idempotency for safe request retries
  • Response Caching with automatic invalidation
  • Rate Limiting (token bucket algorithm)
  • Structured Logging with request tracing
  • Security Headers (Helmet.js)
  • CORS Configuration
  • Health Check Endpoints
  • Graceful Shutdown
  • TypeScript (100% typed)
  • Database Migrations
  • Consistent Error Responses

🎯 The Domain: Reservation System

We've built a reservation system for an inventory store because it demonstrates:

  • Time-sensitive operations - Reservations expire after 10 minutes
  • State transitions - reserved β†’ confirmed β†’ cancelled/expired
  • Concurrency - Multiple users competing for limited inventory
  • Idempotency needs - Network retries can cause double-charging

πŸ—οΈ Architecture

Request β†’ Security Middleware β†’ Rate Limiting β†’ Validation β†’ Route Handler
                                                    ↓
                                              Business Logic
                                                    ↓
                                              Database (SQLite)
                                                    ↓
                                              (Cache/Idempotency)
                                                    ↓
                                              Response

πŸ“š Learning Resources

In This Repository

External Resources

🀝 Contributing

This is a learning repository. Feel free to:

  1. Explore the code
  2. Make changes and break things
  3. Fix issues and learn from them
  4. Add new features

πŸ“„ License

MIT License - See LICENSE file for details


πŸŽ“ Start Learning Now!

Ready to dive in? Continue to the learning documentation:

← Start Learning with docs/01-introduction.md

Or jump straight to a topic:


πŸ’‘ Tip: Every file in this repository is heavily documented. Start exploring and happy learning!

About

A comprehensive, production-ready REST API demonstrating industry best practices for building scalable, maintainable server applications.

Topics

Resources

Stars

Watchers

Forks