How hien2703g/backend works
Project Type and Purpose
• This is a full-stack Node.js/Express “product-management” (e-commerce style) application with two user-facing frontends (Admin and Client), a REST-style backend, real-time chat, file uploads, email notifications, and MongoDB as the data store. It follows an MVC pattern with clear separation between routes, controllers, models, views, and helper services.
Main Architectural Components
A. Browser Frontends (Clients)
• Admin UI: Pug templates under views/admin + static assets (public/Admin).
• Client UI: Pug templates under views/client + static assets (public/Client).
• Socket.io client (chat.js, socket.js) for real-time chat.
B. Node.js Backend
• Entry Point: index.js (creates Express app, HTTP server, attaches Socket.io).
• Routes Layer:
• routes/admin/** – Admin API endpoints
• routes/client/** – Client API endpoints
• Controllers Layer:
• controllers/admin/** – Business logic for admin features (accounts, products, orders, settings, roles, dashboard)
• controllers/client/** – Business logic for client-side features (home, cart, checkout, chat, user, search)
• Middleware Layer:
• Common middleware: body-parser, method-override, cookie/session handling, flash messages
• Admin-specific: auth, settings, file upload to Cloudinary
• Client-specific: auth, cart, category, setting, file upload
• Models Layer:
• Mongoose schemas & models (account, user, role, product, category, cart, order, chat, settings, forgot-password)
• Helper/Service Layer (utility modules in Helper/):
• createTree, pagination, filterStatus, search, email sending (nodemailer), file storage (multer + Cloudinary), slug generation, formatting
C. Data Store
• Primary: MongoDB (connected via Mongoose in config/database.js)
• Seed data JSON: Products_shop.*.json, products-test.*.json
D. External Services
• Cloudinary (images), SMTP (Gmail via nodemailer), Socket.io (real-time chat), possibly third-party libs (moment, express-flash, emoji-picker, file-upload-with-preview, viewerjs)
Relationships and Data Flows
1. Browser → HTTP(S) → Express Routes
2. Routes → Middlewares (auth, upload, validation)
3. Middlewares → Controllers → Helpers/Services as needed
4. Controllers → Models → MongoDB (CRUD operations)
5. Controllers → External Services:
• File upload flows to Cloudinary
• Email flows via nodemailer to SMTP
• Real-time flows via Socket.io channels
6. Responses → Pug views rendered or JSON sent back → Browser
Architectural Patterns & Design Principles
• MVC: clear layering
• Modularization: routes/controllers/models split by feature and by “admin” vs. “client”
• Separation of concerns: helper services for cross-cutting tasks (pagination, file storage, email)
• Real-time event-driven architecture for chat (socket.io) separate from REST flows
• Environment/config management in config/system.js