How hagopj13/node-express-boilerplate works
Main Components
• Client/Consumer: Although not part of the repository, external clients (e.g., browser-based apps, mobile apps, or other services) will call the RESTful endpoints.
• API Gateway (Express Server): The heart of the system where the Express application is set up (see src/app.js and src/index.js). This handles incoming HTTP requests.
• Routes Layer: Located in src/routes (with versioned routes under v1) that maps HTTP endpoints to controller functions.
• Controllers: In src/controllers, these components process incoming requests, call the relevant business logic, and send responses. For example, auth.controller.js and user.controller.js.
• Services Layer: Found in src/services, which contains the core business logic (e.g., authentication, email handling, token management, and user services). These abstract complex operations away from the controllers.
• Models and Data Layer: Inside src/models, Mongoose models (e.g., user.model.js and token.model.js) define the data schema for MongoDB. This includes custom plugins (in src/models/plugins) like paginate and toJSON.
• Middleware: In src/middlewares, which covers authentication, error handling, rate-limiting, input validation, etc. These components intercept requests and responses to augment functionality.
• Configuration and Utilities: Found in src/config and src/utils. You have configuration settings such as logging, passport, tokens, and environment variables. Utilities (ApiError, catchAsync, pick) simplify common code patterns.
• Documentation: The API documentation resources in src/docs that, when the server is running, provide a Swagger-based UI for inspecting endpoints.
• Testing: Tests are organized under tests/ (both integration and unit tests) ensuring system quality and behavior.
Interactions and Relationships
• Externally, a client makes HTTP requests to the Express server.
• The Express server’s routing layer directs the request to the appropriate controller based on the URL path (e.g., /v1/auth/login or /v1/users).
• Controllers process the input and delegate work to service components. In the process, they make use of validation middleware (e.g., using Joi schemas) and check for authentication/authorization via auth middleware.
• Business logic implemented in the services may interact with the database via Mongoose models. Here, custom plugins (like toJSON and paginate) enhance the data layer.
• Logging middleware (using morgan and winston) and error handling middleware capture runtime information and errors.
• For operations that need asynchronous support (for example, sending emails or token generation), the services interact with external systems or internal utilities.
• Finally, a response is sent back to the client, optionally including API documentation endpoints for exploration.
Architectural Patterns & Design Principles
• Layered Architecture: Clear separation of concerns is maintained through controllers (presentation layer), services (business logic layer), and models (data persistence layer).
• Middleware-driven Request Processing: Express middlewares handle authentication, error-handling, rate limiting, and data validation.
• Modular and Pluggable: The use of plugins for Mongoose models and separate modules for validation, logging, and configuration encapsulate functionality.
• Container & Process Management: Dockerfiles and Docker Compose files show readiness for containerized deployment, while PM2 (ecosystem.config.json) handles process management in production.
• Comprehensive Testing: The separation of unit and integration tests reinforces a test-driven approach.
Relevant Technologies and Frameworks
• Technologies: Node.js, Express, MongoDB, Mongoose.
• Authentication: JWT and Passport.
• Validation: Joi.
• Logging: Winston and Morgan.
• Process Management: PM2.
• API Documentation: Swagger (swagger-jsdoc and swagger-ui-express).
• Testing: Jest.
• Containerization: Docker and Docker Compose.
• Code Quality: ESLint, Prettier, and Git hooks via Husky.