How nhatnguyen150100/myceph-server works
Main Components
• Entry Point: The main application (app.js) that bootstraps the Express server.
• Configuration Modules: Under the “config” directory, which includes settings for the database (config.js, connectDB.js), email (mail.config.js), and logging (winston.js). These are key for initializing external services and environment-specific parameters.
• Controllers: Organized under the “controllers” (and its “auth” and “token” subdirectories), these handle requests and invoke the right business logic.
• Services: Located in the “services” folder, these encapsulate the business logic and data interactions, bridging controllers with the database.
• Models: The “models” directory holds Sequelize models corresponding to the database tables. Also included are migration scripts (in “migrations”) which define and modify the DB schema.
• Routes: Under “routers,” where HTTP endpoints are defined and mapped to controller actions.
• Middleware: In the “middleware” directory, which intercepts requests (for tasks like authentication or request validation) before they reach the controllers.
• Logging: Multiple directories (both in “build/src/log” and “src/log”) are dedicated to logging various types of events (e.g., authentication, clinic-related logs, treatment history), which supports monitoring and debugging.
• Common Utilities: Built-in functions and utilities in the “common” folders that support common operations for the whole application.
• Build Process: Note the use of Babel (with the .babelrc file) and scripts in “package.json” to build and deploy the production version.
Relationships and Interactions
• Data Flow: HTTP Clients send requests that hit the Express server via defined routes. These requests pass through middleware (for tasks like validation or recaptcha) and reach controllers.
• Controllers call Services: Controllers extract the required parameters and delegate the business logic to service methods.
• Services and Models: Services interact with the database through Sequelize models. The migrations ensure the models and database schema are aligned.
• Configuration Integration: The config modules are used early in the bootstrapping process (for connecting to the DB, setting up email services, and initializing logger with Winston).
• Logging: Throughout the request lifecycle, various events and errors get logged into dedicated log files to facilitate monitoring.
• Authentication: The auth controllers and token management (with RSA keys and refresh token mechanisms) manage security, and are integrated into the middleware for protecting routes.
Architectural Patterns and Design Principles
• Separation of Concerns: Clearly segmenting configuration, controller logic, service logic, data models, and middleware.
• Modular Design: Each domain (doctors, patients, clinics, treatments, etc.) has dedicated controllers, services, and routes making it easier to scale or modify specific functionalities.
• Use of ORM (Sequelize): To abstract the database interactions, ensuring migration scripts and model definitions are maintained separately from business logic.
• Logging & Error Handling: Integrated logging at various layers to aid in debugging and monitoring in production.
• Security Layers: Implementation of authentication (via passport) and token-based access control using public/private PEM keys.