How muhammadaziz008/product-catalog-api works
Project’s Main Components
• Environment & Configuration:
• .env file (via .env.example) and configuration files in src/configs (e.g., app.js, db.js, token.js). This layer holds environment-specific settings and infrastructure configuration.
• Database & Migrations:
• The migrations directory shows how database tables are created (users, profiles, categories, products, etc.). Indicate that KNEX is used for running migrations and interacting with the database.
• API Layer:
• Controllers (under src/controllers): Handle incoming requests and orchestrate business logic.
• Routers (under src/routers): Define the API routes mapping to controller functions.
• Middlewares (in src/middlewares): Contain authentication guards, role-checking, validations, error handling, etc.
• Business Logic:
• Services (under src/services): Encapsulate the core operations, such as processing orders or handling cart operations.
• Validations (under src/validations): Manage input data validation for different endpoints.
• Utilities:
• Utility functions under src/utils for logging, token management, mailing, OTP generation and error handling.
• Entry Point:
• server.js and src/app.js act as the application’s main entry points where the app is initialized and middleware/routes are connected.
Relationships and Interactions
• Request Flow:
• Client requests hit the API routes (documented in the routers).
• Routes invoke relevant controllers.
• Controllers coordinate with services to execute business logic, validate input data (via validations), and interact with the database via KNEX (also using the configuration from src/configs).
• Middlewares (like auth.guard.js and role.guard.js) intercept requests to enforce security before handing off to controllers.
• Data Persistence:
• Services and controllers interact with the database via queries set up in src/database and through KNEX migrations.
• Utility and Support:
• Utility modules (e.g., mail.js, logger.js, manage.token.js) assist various components for non-business logic tasks.
• External Dependencies:
• The ecosystem.config.cjs and related config files may indicate use of process managers (like PM2) for clustering or process management.
Architectural Patterns and Design Principles
• Layered or Modular Architecture: Separate concerns into configuration, routing, controllers, services, validations, and utilities.
• Clear Separation of Concerns: Each folder/component handles a distinct part of the application (for instance, validations versus business logic).
• Middleware Pattern: Utilized to handle authentication, authorization, and error handling before the request reaches business logic.