How prathamesh3245/js-backend works
1. This repository is a Node.js/Express backend for a video platform, with the architecture centered on authenticated user/account APIs. The main external systems are MongoDB for persistence and Cloudinary for media storage.
2. The runtime entrypoint is src/index.js. It is responsible for bootstrapping the app, loading environment config, connecting to MongoDB through src/db/index.js, and starting the HTTP server. src/app.js defines the Express application itself.
3. src/app.js is the composition root for HTTP concerns: global middleware, CORS/cookie handling, JSON/body parsing, and route mounting. The API is versioned under /api/v1, and the current route surface is organized around src/routes/user.routes.js.
4. The API layer is thin and route-driven. Requests enter Express, are routed by user.routes.js, and then dispatched to controller methods in src/controllers/user.controller.js. There is no separate service layer in this repo; the controller owns most of the business orchestration.
5. Authentication is enforced by src/middlewares/auth.middleware.js. It protects private endpoints by validating JWT access tokens from either cookies or the Authorization header, and it establishes the authenticated user context for downstream handlers.
6. File uploads are handled as a dedicated boundary. src/middlewares/multer.middleware.js accepts multipart form data, staging files under public/temp/ before upload. src/utils/cloudinary.js abstracts pushing those files to Cloudinary, which is the persistent media store for avatars, cover images, and similar assets.
7. The domain model is expressed with Mongoose in src/models/. user.model.js stores account/profile/authentication state, video.model.js models uploaded videos and related metadata, and subscription.models.js captures relationships between users for channel subscriptions. These models are the primary persistence boundary to MongoDB.
8. The controller’s responsibilities include registration, login/logout, token refresh, password change, profile retrieval/update, avatar/cover image updates, channel lookup by username, and watch-history retrieval. The README also indicates aggregation-based queries are used for richer profile/history views, which suggests MongoDB aggregation is important for read endpoints.
9. The data flow for a typical secured profile update is: client sends request with JWT and optionally multipart data, auth middleware validates the user, multer prepares file input if present, the controller uploads media to Cloudinary, updates the Mongoose document in MongoDB, and returns a standardized API response.
10. Cross-cutting utilities live in src/utils/. ApiError and ApiResponse provide a consistent error/response contract, and asyncHandler simplifies async controller error propagation. These utilities keep route handlers small and make the API behavior uniform.
11. Configuration and environment management are externalized through .env and .env.sample. Material runtime settings include the server port, MongoDB URI, JWT secrets/expiries, CORS origin, and Cloudinary credentials. cookie-parser, jsonwebtoken, and bcrypt are key infrastructure libraries supporting auth and session/token handling.
12. From an architecture-diagram perspective, the main boundaries are: HTTP client → Express app → route/middleware layer → controllers → Mongoose models → MongoDB, with a side path from upload middleware/controllers → Cloudinary. The repository currently reflects a backend API foundation with user/account workflows implemented, and domain support for videos and subscriptions already modeled for expansion.
Main entry points: user.controller.js User controller · user.routes.js User routes · app.js Express app · cloudinary.js Cloudinary util
Components 17
Runtime
Process bootstrap and HTTP app startup
- Entry point · Starts the server, loads config, and connects the app to MongoDB
src/index.js - Constants · Shared application constants
src/constants.js
HTTP API
Express app, routes, and request middleware
- Express app · Configures global middleware and mounts versioned routes
src/app.js - User routes · Defines the user/account API surface under /api/v1
src/routes/user.routes.js - Auth guard · Validates access tokens from cookies or headers and sets user context
src/middlewares/auth.middleware.js - Upload gate · Parses multipart requests and stages files in public/temp
src/middlewares/multer.middleware.js
Domain
Controllers and persistence models
- User controller · Orchestrates registration, login, profile, media, and history workflows
src/controllers/user.controller.js - User model · Stores account, profile, auth, and channel state
src/models/user.model.js - Video model · Represents uploaded videos and related metadata
src/models/video.model.js - Subscription model · Captures user-to-user subscription relationships
src/models/subscription.models.js
Integrations
External storage and database boundaries
- DB connector · Initializes the MongoDB connection
src/db/index.js - MongoDB · Primary persistence store for users, videos, and subscriptions
- Cloudinary · Persistent storage for avatars, cover images, and other uploads
Utilities
Shared response, error, and upload helpers
- Cloudinary util · Wraps media upload operations to Cloudinary
src/utils/cloudinary.js - ApiError · Standard application error wrapper
src/utils/ApiError.js - ApiResponse · Standard success response wrapper
src/utils/ApiResponse.js - Async handler · Centralizes async route error propagation
src/utils/asyncHandler.js
Connections 19
- Entry point → Express app: boots · Initializes the Express application and server lifecycle
- Entry point → DB connector: connects · Opens the MongoDB connection before serving requests
- DB connector → MongoDB: uses · Connects the app to MongoDB
- Express app → User routes: mounts · Registers the versioned user API routes
- User routes → Auth guard: protects · Guards private account endpoints
- User routes → Upload gate: parses uploads · Handles multipart requests for profile media
- User routes → User controller: dispatches · Sends requests to the user controller
- User controller → Async handler: wrapped by · Uses async wrapper to centralize errors
- User controller → ApiResponse: returns · Formats successful API responses
- User controller → ApiError: throws · Signals application errors in a standard shape
- User controller → User model: reads/writes · Manages accounts and profile state
- User controller → Video model: reads · Loads video-related history and metadata
- User controller → Subscription model: reads · Uses subscriptions for channel/profile views
- User controller → Cloudinary util: uploads media · Sends avatar and cover files to Cloudinary
- Cloudinary util → Cloudinary: stores · Persists uploaded media externally
- Upload gate → Cloudinary util: prepares files · Stages multipart uploads for Cloudinary transfer
- Express app → Constants: configures · Uses shared constants during app setup
- User controller → Auth guard: depends on · Controller assumes authenticated user context on secured actions
- User controller → MongoDB: persists via models · All model operations ultimately hit MongoDB