How thanhhai1011/watch-store works
Project Type and Purpose
• This is a full-stack e-commerce application ("watch-store") with two main user roles (customers & admin).
• Frontend: a React/Redux single-page app with public pages, user account flows, shopping cart, chat, and an admin console.
• Backend: Node.js/Express REST + WebSocket API, MVC pattern, with controllers/routers/models, connecting to a database and a few external services (Cloudinary, GHN shipping, VnPay payment gateway).
Main Components
a. Frontend (React/Redux SPA)
• UI components: pages (Home, ProductDetail, Cart, Order, Chat, Admin), shared components (Header, Footer, Alerts).
• State management: Redux store + actions + reducers.
• API client services: config.services.js, Axios (or fetch) wrappers.
• WebSocket client for real-time chat (socket.io).
b. Backend (Node.js & Express)
• API server entrypoint: index.js
• Configuration layers: db/db.js (database connection), cloudinary.js (media upload), socket.js (WebSocket setup).
• Controllers: business logic per domain (UserController, ProductController, OrderController, ChatController, etc.).
• Routers: REST endpoints organized by resource (UserRouter, ProductRouter, OrderRouter, etc.).
• Models: database schemas for Users, Products, Orders, Messages, Conversations, ListTypeProduct, SelectList.
• Utils: shared helpers (until.js).
c. Database
• Primary data store (e.g., MongoDB or SQL) for all models.
d. WebSocket Service
• Real-time channel (socket.io) for chat messages and conversation events.
e. External Services
• Cloudinary: image/media storage.
• GHN: shipping API integration.
• VnPay: payment gateway (redirect flow, success callback).
Relationships & Data Flow
• The React client issues HTTP requests (REST/JSON) to the Express API for CRUD operations (products, users, orders, lists).
• On checkout, the backend coordinates with VnPay (redirect → callback) and GHN (shipping rates & label creation).
• File uploads from the client go through the API to Cloudinary (images for products).
• The client opens a WebSocket connection to the same server (socket.js) to send/receive chat messages; the server persists messages to the DB.
• Admin and customer UIs share the API but enforce role-based access (via JWT/session middleware in UserController).
Architectural Patterns & Design Principles
• MVC on the server: routers → controllers → models.
• Three-tier architecture: Presentation (React), Application (Express business logic + WebSocket), Data (DB).
• Redux on the client: unidirectional data flow for UI state.
• Layered configuration: separation of concerns for database, cloudinary, and socket.
• REST for resource operations, WebSocket for real-time.
• Integration points clearly externalized (GHN & VnPay clients encapsulated in controllers).
Final Advice
• Separation of concerns (frontend vs. backend vs. services).