How jekoyu/express_master works
Project Type and Purpose
• This is a Node.js/Express REST API service for user account management and authentication (admin & agent roles).
• It follows a layered, modular architecture separating HTTP routing, business logic, data access, configuration, and utilities.
File‐Structure Analysis
• Entry point:
• bin/www (HTTP server bootstrap)
• app.js (Express app setup: middleware & routes)
• Configuration:
• /config/*.js (environment‐driven settings: DB, AES, tokens, email, API)
• .env-sample
• Database drivers:
• /database/mysql.connection.js
• /database/redis.connection.js
• Application core (/src):
• /route → Express routes
• /controller → Orchestrate request→response
• /service → Business logic / token generation
• /repository → DB/Redis queries
• /model → Schema definitions (MySQL ORM models)
• /validation → Input schemas
• /constant → Shared enums/values
• Cross-cutting:
• /middleware → Request validators, auth guards, custom headers
• /exception → Structured error classes
• /utility → Helpers (encryption, pagination, formatting)
• Other:
• /dev-notes for DB DDL scripts
• package.json, .gitignore, node_modules
Key Architectural Patterns
• Layered (Route → Controller → Service → Repository → Model)
• Dependency Injection via require/import of config and utilities
• Centralized error handling with custom exception types
• Environment‐based configuration
a. External Actors (Clients)
• API Consumers (Web, Mobile, External Systems)
b. API Layer
• Load Balancer / API Gateway (optional)
• Express App
• Middleware (header injector, input validator, JWT guard)
• Routes → Controllers
c. Business Layer
• Services: authService, accountAdminService, tokenService, etc.
d. Data Access Layer
• Repositories: SQL queries (MySQL) and Redis operations
• Models: ORM mappings (account, role, status)
e. Data Stores & External Systems
• MySQL database
• Redis cache/session store
• SMTP/email provider (via email.config)
f. Configuration Subsystem
• Config modules reading from .env (db.config, token.config, aes.config)
g. Utility Modules
• Encryption, pagination, metadata, response formatting
Interactions & Data Flow
1. Client → HTTP request → Express app
2. Middleware chain (UTC header → request validation → token auth)
3. Router dispatch → Controller method
4. Controller calls Service
5. Service enforces business rules, calls Repository for persistence/caching
6. Repository uses MySQL/Redis drivers
7. Service returns result to Controller → formatted via response-API utility
8. Controller sends HTTP response
9. Errors bubble up to central error handler using custom exception classes
10. Services may trigger external email via configured SMTP
Technologies & Frameworks
• Node.js, Express.js
• MySQL (ORM or raw SQL)
• Redis
• JSON Web Token (token.config)
• AES encryption (aes.config & encrypt-decrypt utility)
• dotenv for env vars
• Custom middleware & error classes
Essential Elements
• Clear labels on components (e.g., “Auth Controller”)