Overview
The E-commerce API follows a layered architecture pattern, separating concerns across controllers, services, repositories, and middleware. This design promotes maintainability, testability, and scalability.Architecture Layers
Folder Structure
The backend source code is organized as follows:Directory Responsibilities
Controllers
Controllers
Handle HTTP requests and responses. Controllers receive validated data from middleware, delegate business logic to services, and format responses.Files:
auth.controller.ts- User registration and logincart.controller.ts- Shopping cart operationscategory.controller.ts- Product category managementorder.controller.ts- Order processingproduct.controller.ts- Product CRUD operations
Services
Services
Contain business logic and orchestrate operations across multiple repositories. Services are responsible for data validation, transformation, and complex workflows.Example:
auth.services.ts handles password hashing, JWT generation, and user validation.Repositories
Repositories
Abstract database operations using Prisma. Each repository corresponds to a domain entity (User, Product, Cart, Order, Category).Files:
user.repository.tsproduct.repository.tscart.repository.tsorder.repository.tscategory.repository.ts
Middleware
Middleware
Process requests before they reach controllers. Middleware handles cross-cutting concerns like authentication, authorization, validation, and error handling.Files:
auth.middleware.ts- JWT token verificationrole.middleware.ts- Role-based access controlvalidation.middleware.ts- DTO validation using class-validatorerror.middleware.ts- Centralized error handling
DTOs (Data Transfer Objects)
DTOs (Data Transfer Objects)
Define the shape and validation rules for incoming request data using class-validator decorators.Files:
auth.dto.tscart.dto.tscategory.dto.tsproduct.dto.ts
Application Configuration
The Express application is configured inapp.ts with the following middleware stack:
backend/src/app.ts
Request Flow Example
Let’s trace a request to create a new product:Middleware chain executes
- CORS & Helmet - Security headers and origin validation
- JSON Parser - Parse request body
- authMiddleware - Verify JWT token (
auth.middleware.ts:7) - adminOnly - Check user has admin role (
role.middleware.ts:37) - validateDto - Validate against CreateProductDto (
validation.middleware.ts:8)
Service processes business logic
ProductService handles domain logic (e.g., checking stock, formatting data)Error Handling
The API uses centralized error handling through theerrorHandler middleware:
backend/src/middleware/error.middleware.ts
Custom Error Classes
The API defines domain-specific errors:ConflictError(409) - Resource already existsUnauthorizedError(401) - Invalid credentialsNotFoundError(404) - Resource not foundAppError- Base error class
Configuration Management
Environment variables are validated and centralized inconfig/index.ts:
backend/src/config/index.ts
Next Steps
Database Schema
Explore the complete Prisma schema and data models
Authentication
Learn how JWT authentication is implemented
Authorization
Understand role-based access control
API Reference
View all available endpoints