How vaishnavinandigama/coupon-backend works
Purpose and Entry Points
This is a Node.js backend organized as a layered API for authentication, orders, coupons, and dashboard reporting. Process startup and HTTP application assembly are separated:
• src/server.js
• src/app.js
HTTP Routing Boundary
Feature-specific route modules expose the API surface and delegate request handling to controllers. This keeps endpoint registration separate from business logic:
• src/routes/authRoutes.js
• src/routes/orderRoutes.js
• src/routes/couponRoutes.js
• src/routes/dashboardRoutes.js
Controller Layer
Controllers form the HTTP boundary: they receive route requests, invoke services, and return responses. The domain controller set mirrors the route modules:
• src/controllers/authController.js
• src/controllers/orderController.js
• src/controllers/dashboardController.js
Service / Business Logic Layer
Services encapsulate domain operations independently of HTTP concerns. Primary control flow is route → controller → service → model/external integration:
• src/services/authService.js
• src/services/orderService.js
• src/services/couponService.js
• src/services/dashboardService.js
Persistence and Domain Models
User, order, and coupon records are represented by dedicated model modules. Services use these models as the persistence boundary; dashboard behavior likely aggregates order/user/coupon data rather than owning a separate model:
• src/models/User.js
• src/models/Order.js
• src/models/Coupon.js
Database initialization/configuration is isolated from domain code:
• src/config/db.js
Authentication, Authorization, and Upload Boundaries
Authentication is implemented across auth routes/controllers/services, with token generation isolated as a utility. Middleware protects authenticated and administrative paths before controllers execute. Upload handling is a separate middleware concern, with Cloudinary configured as the external media service:
• src/middlewares/authMiddleware.js
• src/middlewares/adminMiddleware.js
• src/middlewares/uploadMiddleware.js
• src/utils/generateToken.js
• src/config/cloudinary.js
Primary Flows and Infrastructure
Authentication flow: auth route → controller → auth service → User model → token generation, with subsequent requests validated by auth middleware. Order and coupon flows follow their matching route/controller/service/model chains; admin middleware likely gates privileged coupon and dashboard operations. Database access is centralized through src/config/db.js; media uploads cross the application boundary through src/config/cloudinary.js.
Components 15
HTTP API
- HTTP app assembly
src/app.js - Feature routes · Auth, order, coupon, and dashboard endpoint registration.
- Auth routes
src/routes/authRoutes.js - Order routes
src/routes/orderRoutes.js - Coupon routes
src/routes/couponRoutes.js - Dashboard routes
src/routes/dashboardRoutes.js - Controllers · Auth, order, and dashboard request handlers.
- Access & upload middleware · Authentication, admin authorization, and upload handling.
- Upload middleware
src/middlewares/uploadMiddleware.js
Domain Logic
- Domain services · Auth, order, coupon, and dashboard operations.
- Token generation
src/utils/generateToken.js
Persistence & Integrations
- User, order & coupon models · Persistence boundary for core records.
- Database configuration
src/config/db.js - Cloudinary integration
src/config/cloudinary.js
Other components
- Server startup
src/server.js
Connections 16
- Server startup → HTTP app assembly: starts
- HTTP app assembly → Feature routes: mounts
- Feature routes → Auth routes: includes
- Feature routes → Order routes: includes
- Feature routes → Coupon routes: includes
- Feature routes → Dashboard routes: includes
- Auth routes → Access & upload middleware: protects requests
- Order routes → Access & upload middleware: protects requests
- Coupon routes → Access & upload middleware: admin-gated
- Dashboard routes → Access & upload middleware: admin-gated
- Feature routes → Controllers: dispatches
- Controllers → Domain services: invokes
- Domain services → User, order & coupon models: reads and writes
- Domain services → Token generation: issues auth tokens
- User, order & coupon models → Database configuration: uses
- Upload middleware → Cloudinary integration: uploads media