GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com โ€” GitHub - codezelaca/se-node-server-rest-apis: A comprehensive, production-ready REST API demonstrating industry best practices for building scalable, maintainable server applications.

BranchesTags
Open more actions menu

Latest commit

 

History

8 Commits
8 Commits

Folders and files

NameName
Last commit message
Last commit date
docs
docs
 
 
frontend
frontend
 
 
src
src
 
 
tests
tests
 
 
.env.example
.env.example
 
 
.eslintrc.json
.eslintrc.json
 
 
.gitignore
.gitignore
 
 
.prettierrc.json
.prettierrc.json
 
 
LEARNING_CHECKLIST.md
LEARNING_CHECKLIST.md
 
 
POSTMAN_COLLECTION.md
POSTMAN_COLLECTION.md
 
 
POSTMAN_GUIDE.md
POSTMAN_GUIDE.md
 
 
README.md
README.md
 
 
docker-compose.yml
docker-compose.yml
 
 
package.json
package.json
 
 
postman-collection.json
postman-collection.json
 
 
postman-environment.json
postman-environment.json
 
 
tsconfig.json
tsconfig.json
 
 
vitest.config.ts
vitest.config.ts
 
 

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}'

๐ŸŽจ Frontend Application

A modern Next.js 16 frontend with glass-morphism UI is included in the frontend/ directory.

Frontend Stack

Component Technology
Framework Next.js 16 (App Router)
Language TypeScript 5
Styling Tailwind CSS 4
UI Library shadcn/ui
State TanStack Query
Forms React Hook Form + Zod

Running the Frontend

# Navigate to frontend directory
cd frontend

# Install dependencies
npm install

# Set up environment
cp .env.example .env.local

# Start development server (runs on port 3001)
npm run dev

The frontend will be available at http://localhost:3001

Frontend Features

  • โœ… Glass-morphism Design - Modern translucent UI with backdrop blur
  • โœ… Real-time Updates - TanStack Query for live data synchronization
  • โœ… Form Validation - Zod schemas matching backend validation
  • โœ… Idempotency - Automatic Idempotency-Key header generation
  • โœ… Error Handling - Request ID display for debugging
  • โœ… Responsive - Mobile-first design

๐Ÿ“– frontend/README.md - Complete frontend documentation


๐Ÿ“š 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_GUIDE.md - Complete Postman testing instructions
  • LEARNING_CHECKLIST.md - Track your learning progress

Postman Collection

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

  • postman-collection.json - Import this into Postman
  • postman-environment.json - Environment variables

๐Ÿ› ๏ธ The Stack

Backend

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

Frontend

Component Technology Why?
Framework Next.js 16 React 19, App Router, Turbopack
Styling Tailwind CSS 4 Utility-first, JIT compiler
UI Components shadcn/ui Accessible, customizable
State Management TanStack Query Server state, caching
Forms React Hook Form Performance, flexibility
Validation Zod Type-safe schema validation

๐Ÿ“ 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
โ”‚
โ”œโ”€โ”€ frontend/                       # Next.js 16 Frontend Application
โ”‚   โ”œโ”€โ”€ src/                        # Frontend source
โ”‚   โ”‚   โ”œโ”€โ”€ app/                    # Next.js App Router
โ”‚   โ”‚   โ”œโ”€โ”€ components/             # React components
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ layout/             # Layout components
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ui/                 # shadcn/ui components
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ui-blocks/          # Custom UI blocks
โ”‚   โ”‚   โ””โ”€โ”€ lib/                    # Utilities
โ”‚   โ”‚       โ”œโ”€โ”€ api/                # API client
โ”‚   โ”‚       โ””โ”€โ”€ query/              # TanStack Query
โ”‚   โ”œโ”€โ”€ .env.example                # Frontend env template
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ””โ”€โ”€ README.md                   # Frontend documentation
โ”‚
โ”œโ”€โ”€ 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
  • Background jobs - Automatic expiration with stock restoration (30s interval)

๐Ÿ—๏ธ Architecture

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

๐Ÿ“š Learning Resources

In This Repository

  • docs/README.md - Complete learning guide
  • Each source file has extensive comments
  • Type definitions in src/types/index.ts for reference

External Resources

  • Express.js Guide
  • Zod Documentation
  • Node.js Best Practices
  • REST API Tutorial

๐Ÿค 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:

  • Validation โ†’
  • Concurrency โ†’
  • Idempotency โ†’
  • Caching โ†’
  • Logging โ†’

๐Ÿ’ก 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

api-restsoftware-engineering

Resources

Readme
Activity
Custom properties

Stars

0 stars

Watchers

0 watching

Forks

2 forks
Report repository

Contributors

Languages

You are visitor 01402775 โ€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร—600. No cookies required.