Skip to content

Project Structure Documentation

This document comprehensively describes the organization and purpose of key files and folders within your NestJS application, providing a clear understanding of their roles and contributions.

txt
.
└── project root/
    └── src/
        ├── auth/
        │   ├── auth.controller.ts
        │   ├── auth.dto.ts
        │   ├── auth.modal.ts
        │   ├── auth.module.ts
        │   ├── auth.service.ts
        │   ├── jwt-auth.guard.ts
        │   └── jwt.strategy.ts
        ├── email/
        │   ├── email.controller.ts
        │   └── email.service.ts
        ├── modules/
        │   └── <Resources>/
        │       ├── <Resource>.controller.ts
        │       ├── <Resource>.model.ts
        │       ├── <Resource>.module.ts
        │       ├── <Resource>.service.ts
        │       ├── create-<Resource>.dto.ts
        │       └── update-<Resource>.dto.ts
        ├── shared/
        │   ├── constants/
        │   │   └── index.ts
        │   ├── decorator/
        │   │   ├── public.ts
        │   │   └── validator.ts
        │   ├── interceptor/
        │   │   └── response.interceptor.ts
        │   └── utils/
        │       └── common.ts
        ├── app.controller.ts
        ├── app.module.ts
        ├── app.service.ts
        └── main.ts

Top-Level Folders:

  1. assets: (Optional)

    • email-templates: (Optional)
      • Contains HTML templates used for generating email content (e.g., verification, password reset).
      • Typically utilized by the email module or related services for email sending.
      • File examples: email-verify.hbs, reset-password.hbs.
  2. src: (Core application source code)

    • auth:
      • Encompasses functionalities related to user authentication and authorization.
      • Includes:
        • auth.controller.ts: Defines controllers for handling authentication-related API requests (e.g., login, registration).
        • auth.dto.ts: Contains Data Transfer Objects (DTOs) for authentication data in requests and responses.
        • auth.model.ts: Defines the Mongoose model schema for representing users or authentication-related data.
        • auth.module.ts: The NestJS module definition for authentication, importing and exporting necessary components.
        • auth.service.ts: Implements business logic and interactions with user data or external authentication providers.
        • jwt-auth.guard.ts: Provides JWT-based authentication guards for protecting secure routes.
        • jwt.strategy.ts: Defines the JWT authentication strategy for verifying and extracting user information from JWT tokens.
    • email:
      • Handles email sending functionalities.
      • Includes:
        • email.controller.ts: Defines controllers for email-related API requests (e.g., sending emails).
        • email.service.ts: Provides services for sending email notifications or triggering email sending logic.
    • modules:
      • Contains individual modules for specific entities or resources within your application, following a consistent structure:
        • <Resource>.controller.ts: Defines controllers for handling API requests related to the entity (e.g., blog.controller.ts).
        • <Resource>.model.ts: Defines the Mongoose model schema for the entity (e.g., blog.model.ts).
        • <Resource>.module.ts: The NestJS module definition for the entity, importing and exporting relevant components.
        • <Resource>.service.ts: Implements business logic and data access operations related to the entity (e.g., blog.service.ts).
        • create-<Resource>.dto.ts: Defines the DTO used for creating new instances of the entity (e.g., create-blog.dto.ts).
        • update-<Resource>.dto.ts: Defines the DTO used for updating existing instances of the entity (e.g., update-blog.dto.ts).
    • shared:
      • Provides reusable components across the application:
        • constants: Stores fixed values and configurations used throughout the application (e.g., API endpoints, roles).
        • decorators: Custom decorators for controllers, services, or entities to enforce access control, validation, or other functionalities.
        • interceptors: Classes for intercepting and modifying HTTP requests and responses (e.g., logging, response formatting).
        • utils: Common utility functions used in various parts of the application (e.g., data manipulation, validation).
    • app.controller.ts: Defines the main application controller, handling general or root-level API requests.
    • app.module.ts: The root module of the application, importing all other modules and configuring services.
    • app.service.ts: (Optional) Provides an optional global service for application-wide logic.
    • main.ts: The entry point for running the NestJS application.

Benefits of this Structure:

  • Modularity and Organization: Promotes a well-organized codebase, dividing functionalities into reusable and maintainable modules.
  • Dependency Management: Simplifies dependency management by grouping related components within modules.
  • Clarity and Understanding: Makes the project structure clearer, aiding in development, testing, and collaboration.
  • Reusability: Encourages code reuse across modules, preventing code duplication and inconsistencies.