How kietvo1248/cinema-backend-system works
Project Type and Purpose
• This is a full-stack backend service (RESTful API) for a cinema system, written in Node.js/Express, persisting data in MongoDB, and secured via JWT. It exposes a set of HTTP endpoints for user auth, bookings, movie information, payments, promotions, and admin management.
File Structure Analysis
• Top-level:
• Dockerfile, .env.example → containerization & env config
• server.js (or src/app.js) → application entry point
• package.json / package-lock.json → dependencies & scripts
• swagger.js → API documentation setup
• src/config: external-service configurations (DB, Cloudinary, Cosmic CMS, multiple payment gateways)
• src/middleware: authorization layers by role (admin, employee, team, general auth)
• src/models: Mongoose schemas for all domain entities (User, Booking, Movie, Room, Invoice, etc.)
• src/routes: grouped by feature area (authentication, admin, booking, movie, payments, product, promotions, theater, userFeature)
Key Architectural Patterns
• Layered MVC-style division:
• Routes → Route definitions & validation
• Middleware → Auth, role checks
• Models → Data persistence layer (Mongoose)
• Controllers (inline in routes or in app.js) → Business logic
• Dependency injection of config objects
• External integrations via dedicated config modules
• JWT token-based stateless authentication
Main Components
a. Client (Web/Mobile)
b. API Gateway / Load Balancer (if using Docker + multiple replicas)
c. Node.js / Express App
– Route Layer (grouped feature modules)
– Middleware Layer (auth, role enforcement)
– Business Logic / Controllers
– Data Access Layer (Mongoose Models)
d. MongoDB Database (Atlas or local)
e. External Services (Cloudinary for media, Cosmic CMS for content, PayOS, VNPay gateway, Cash-on-delivery endpoint)
f. Swagger Docs (API documentation)
g. Docker Container & .env secrets store
Component Relationships & Data Flow
• Route dispatches to Middleware chain (JWT verification → role middleware) → Controller.
• Controller calls Mongoose Model methods → MongoDB (read/write).
• For upload, Controller calls Cloudinary.
• For CMS content, Controller calls Cosmic API.
• For payments, Controller calls PayOS/VNPay external endpoints → handles callbacks/webhooks.
• Logger/Counter model for metrics, Invoice generation, emailing (if any).
• Swagger.js serves at /api-docs.
Architectural Patterns & Design Principles
• Separation of Concerns: each folder handles a distinct responsibility
• Single Source of Truth: .env for secrets, config/ modules for external integrations
• Role-Based Access Control: via distinct middleware per role (admin, employee, team)
• Modular Routing: feature-based route modules, easy to extend
• Stateless Auth: JWT tokens, no server session storage