|
| 1 | +# Node.js REST API Reference Architecture |
| 2 | + |
| 3 | +> **A production-ready, highly optimized, and modular Node.js REST API blueprint built with TypeScript.** |
| 4 | +
|
| 5 | +This repository serves as a "masterclass" reference for architecting professional-grade backends. It demonstrates a clean separation of concerns, robust security practices, and a scalable project structure that follows the absolute latest industry standards. |
| 6 | + |
| 7 | +## 🚀 Architectural Highlights |
| 8 | + |
| 9 | +- **TypeScript-First Architecture**: Strictly typed from the ground up, utilizing modern `NodeNext` module resolution for high performance and compatibility. |
| 10 | +- **Native Environment Management**: Utilizes the Node.js built-in `--env-file` flag (introduced in v20.6.0), eliminating the need for external packages like `dotenv` and reducing the application's dependency footprint. |
| 11 | +- **Modular Layered Design**: Follows the **Route -> Controller -> Service** pattern to ensure testability and clear responsibility boundaries. |
| 12 | +- **Centralized Config Management**: Validates environment variables at startup, preventing runtime failures due to missing configuration. |
| 13 | +- **Enterprise Middleware Stack**: |
| 14 | + - **Async Handler**: Centralized error catching for all async routes. |
| 15 | + - **Validation Middleware**: Decoupled, reusable input validation using `express-validator`. |
| 16 | + - **Rate Limiting**: Integrated pattern for protecting endpoints from abuse. |
| 17 | + - **Global Error Handler**: Standardized JSON responses for all operational and system errors. |
| 18 | +- **Security-Hardened**: Pre-configured with: |
| 19 | + - **Helmet**: Essential security headers. |
| 20 | + - **CORS**: Granular cross-origin resource sharing. |
| 21 | +- **Automated Testing Masterclass**: |
| 22 | + - **Jest & Supertest**: Integrated integration testing for API endpoints. |
| 23 | + - **ESM Test Runner**: Configured to support modern TypeScript ESM module resolution. |
| 24 | +- **Cloud-Native & DevOps Ready**: |
| 25 | + - **Dockerized**: Multi-stage Dockerfile for optimized production images. |
| 26 | + - **GitHub Actions**: Automated CI pipeline for testing, auditing, and building. |
| 27 | + - **Google Cloud Build**: Pre-configured `cloudbuild.yaml` for GCP deployments. |
| 28 | + - **Clean Architecture Testing**: Demonstrates testing controllers and services in isolation from infrastructure. |
| 29 | + - **Input Sanitization**: Express-validator integration for defensive programming. |
| 30 | + - **Powered-By Suppression**: Hidden server identification. |
| 31 | +- **Performance Optimized**: Includes HTTP compression and lightweight request logging via Morgan. |
| 32 | +- **Standardized Error Handling**: Centralized middleware for uniform error responses across the entire API. |
| 33 | + |
| 34 | +## 📁 Project Structure |
| 35 | + |
| 36 | +``` |
| 37 | +src/ |
| 38 | +├── api/ |
| 39 | +│ ├── controllers/ # Business logic orchestration |
| 40 | +│ ├── middlewares/ # Global and route-specific guards |
| 41 | +│ ├── routes/ # Endpoint definitions and validation mapping |
| 42 | +│ └── services/ # Core domain logic and third-party integrations |
| 43 | +├── core/ |
| 44 | +│ ├── config/ # Environment and constant management |
| 45 | +│ └── utils/ # Reusable helpers (Loggers, Formatters) |
| 46 | +├── app.ts # Express application setup |
| 47 | +└── index.ts # Server entry point |
| 48 | +``` |
| 49 | + |
| 50 | +## 🛠️ Tech Stack |
| 51 | + |
| 52 | +- **Runtime**: Node.js (v24+ recommended) |
| 53 | +- **Framework**: Express.js |
| 54 | +- **Language**: TypeScript |
| 55 | +- **Validation**: Express-validator |
| 56 | +- **Security**: Helmet, CORS |
| 57 | +- **Logging**: Morgan |
| 58 | + |
| 59 | +## 🚦 Getting Started |
| 60 | + |
| 61 | +### Prerequisites |
| 62 | + |
| 63 | +- Node.js ^24.0.0 |
| 64 | +- npm or yarn |
| 65 | + |
| 66 | +### Installation |
| 67 | + |
| 68 | +1. Clone the repository: |
| 69 | + ```bash |
| 70 | + git clone <repo-url> |
| 71 | + cd node-rest-reference-architecture |
| 72 | + ``` |
| 73 | + |
| 74 | +2. Install dependencies: |
| 75 | + ```bash |
| 76 | + npm install |
| 77 | + ``` |
| 78 | + |
| 79 | +3. Configure Environment Variables: |
| 80 | + Create a `.env` file based on the provided patterns. |
| 81 | + |
| 82 | +### Development |
| 83 | + |
| 84 | +Run the development server with automatic reloading: |
| 85 | +```bash |
| 86 | +npm run dev |
| 87 | +``` |
| 88 | + |
| 89 | +### Build |
| 90 | + |
| 91 | +Compile the TypeScript source into production-ready JavaScript: |
| 92 | +```bash |
| 93 | +npm run build |
| 94 | +``` |
| 95 | + |
| 96 | +## 📜 License |
| 97 | + |
| 98 | +This project is open-sourced under the MIT License. |
0 commit comments