How tuandangvan/my-pets-api works
Main Components
• API Server: The central Express.js server (see src/server.js) which is responsible for routing incoming HTTP requests.
• Routes: Located in src/routes/v1, these are grouped by functionality (e.g., auth, admin, pet, center, chat, order, user, and post). Each route file maps specific endpoints to controller methods.
• Controllers: Found in src/controllers, controllers handle the incoming requests and delegate to the business logic in services. There are separate controllers for various domains such as authentication, pets, centers, chats, posts, etc.
• Services: Contained in src/services, these modules encapsulate business logic (e.g., accountService, adoptService, petService) and interact with the data layer.
• Database / Models: The models in src/models represent MongoDB collections. The database configuration is handled in src/config/mongodb.js.
• Middlewares: These include authentication and error handling middleware located in src/middlewares, ensuring that requests are properly authorized and handled for errors.
• Authentication & Token Utilities: Components like src/auth/authenticationToken.js and utility files (e.g., jwtUtils.js, token.js) work together for token generation and validation using JWT.
• Third-Party Integrations:
• Email Service: (src/sendEmail/emailService.js) which handles mailing functionalities (e.g., OTP codes).
• Upload Service: (src/upload/uploadCloudinary.js) for handling file uploads to Cloudinary.
• Socket Server: (src/socketServer.js) likely for real-time messaging or notifications.
Relationships and Interactions
• Incoming HTTP requests reach the Express server, which then routes calls to the appropriate route modules.
• Each route is connected to a controller method that processes the request.
• Controllers delegate operations to corresponding service functions (e.g., petService for pet-related logic).
• Services interact with the database via Mongoose models.
• Middlewares (for authentication, authorization, and error handling) are applied at various stages in the request lifecycle.
• Utility modules are used throughout to support functionalities like token generation and error formatting.
• Integration points for external services (email, file upload, real-time notifications) are clearly defined.
Architectural Patterns and Design Principles
• Layered Architecture: The clear separation between routes, controllers, services, and models enables better maintainability and testability.
• Modularity: Each feature (e.g., authentication, pet management, messaging) has its own folder grouping related controllers, routes, and services.
• Dependency Injection and Middleware Pattern: Middlewares ensure pre-processing (e.g., authentication) before reaching the core business logic.
• Reusability: Utility functions and constants (in src/utils and src/enums) reduce code duplication.