How fifocode/nodejs-backend-architecture-typescript works
Project type and purpose
• This is a production-ready Node.js backend API written in TypeScript that implements a blogging platform (similar to Medium or FreeCodeCamp).
• It exposes REST endpoints for user management (signup, login, profile), blog creation/editing, blog listing, and protected routes, with JWT and API-Key based authentication.
File structure
Top-level directories:
• src/
– server.ts (HTTP server entry point)
– app.ts (Express app setup: middleware, routes, error/response handlers)
– auth/ (authentication, authorization, API-Key & JWT utilities)
– routes/ (feature-based routing modules: access, blog, blogs, profile)
– database/ (Mongoose models + repository pattern)
– cache/ (Redis cache layer + repository)
– core/ (global error/response, JWT helper, logger)
– helpers/ (asyncHandler, validators, permissions, security utils)
• tests/ (unit + integration tests per module)
• Dockerfile + docker-compose.yml (app, MongoDB, Redis, tester)
• .templates/ (code scaffolding templates)
Key insights from README
• Uses a 3RE architecture: Router → RouteHandler → ResponseHandler → ErrorHandler
• Centralized error & response framework for consistent API patterns
• Feature encapsulation: each feature (auth, blog, user) has its own route, schema, repository
• Caching of read-heavy data in Redis
• MongoDB (via Mongoose) for persistence
• JWT for stateless session, API-Key for service-level access
• Docker Compose orchestrates services
Main components
1. Clients
– Web or Mobile Frontend
– Postman / External Consumers
2. Load Balancer (optional, e.g., NGINX or AWS ELB)
3. API Layer (Express.js application)
a. API-Key Authentication Module
b. JWT Authentication / Authorization Module
c. Router Module (routes/index.ts)
d. Route Handlers per feature (access, blog, blogs, profile)
e. Centralized ResponseHandler & ErrorHandler (core/ApiResponse, core/ApiError)
f. Logging (core/Logger)
4. Business Logic Layer
– Validators & Helpers (helpers/validator, asyncHandler, permission)
5. Persistence Layer
a. MongoDB (collections: Users, Blogs, ApiKeys, Roles, Keystore)
• Mongoose Models (database/model/*.ts)
• Repository Pattern (database/repository/*Repo.ts)
b. Redis Cache
• Cache Keys & Queries (cache/keys.ts, cache/query.ts)
• Cache Repositories (cache/repository/*Cache.ts)
6. Key Management
– RSA key-pair for JWT signing (keys/private.pem, public.pem)
7. Infrastructure & DevOps
– Docker containers: app, MongoDB, Redis, tester
– Docker Compose orchestration
– CI/CD pipeline (GitHub Actions workflow)
8. Tests
– Unit & Integration tests invoking the API inside a tester container
Relationships & data flow
1. Client → Load Balancer → Express.js “server.ts”
2. Express Middleware:
• API-Key auth → JWT auth → Router
3. Router → RouteHandler → Helpers/Validators → Repositories
4. Repository →
• First checks Redis Cache → if hit, return cached data
• Else queries MongoDB via Mongoose → returns result → optionally writes back to Redis
5. RouteHandler → ResponseHandler → Client
6. Errors propagate to ErrorHandler → formatted error response
7. All requests & errors logged by Logger
Architectural patterns & design principles
• Layered Architecture (Presentation → Business Logic → Data Access)
• Feature-based modularization (each feature folder contains its controller, schema, repository).
• Repository Pattern for DB & Cache abstractions
• Centralized response & error handling (consistent response envelope).
• Async/await non-blocking calls
• Separation of concerns (auth logic, caching logic, DB logic, routing are all decoupled)
Technologies & frameworks
• Node.js + Express.js + TypeScript
• Mongoose (MongoDB ODM)
• Redis (caching)
• JWT (stateless auth) + API-Key
• Joi (request validation via schema.ts files)
• Docker & Docker Compose
• Jest (unit/integration testing)