How madan-ram-sai/youtube-clone-backend works
Purpose and Entry Points
This is a Node.js backend for a YouTube-like service, centered on user accounts, videos, and subscriptions. Process startup is split between index.js and src/index.js; the Express application is assembled in src/app.js.
Application Composition
src/app.js configures the HTTP application and mounts route modules. The current exposed API surface is user-focused through src/routes/user.routes.js, with request handling implemented in src/controllers/user.controller.js.
Request and Authentication Boundary
Protected endpoints pass through JWT-style authentication middleware in src/middlewares/auth.middleware.js. This middleware establishes the authenticated user context before controllers execute, separating identity verification from account business logic.
User Domain
src/models/user.models.js defines persisted user/account data, while src/controllers/user.controller.js owns account workflows such as registration, login, logout, token refresh, profile access/update, and avatar/cover-image changes. src/routes/user.routes.js maps these workflows to HTTP endpoints.
Media Upload Pipeline
Multipart uploads are accepted via src/middlewares/multer.middleware.js, using public/temp as temporary local storage. Controllers then delegate durable media hosting to src/utils/cloudinary.js; this creates a boundary between local request-file handling and Cloudinary-hosted avatar, cover-image, or video assets.
Persistence and Content Models
MongoDB connectivity is initialized in src/db/index.js. Mongoose-style domain models represent users, videos, and subscriptions in src/models/user.models.js, src/models/video.models.js, and src/models/subscription.model.js. Videos and subscriptions are modeled independently from the currently routed user API, indicating planned or partial content/social features.
Error and Response Flow
Async controller failures are normalized by src/utils/asyncHandler.js; application/domain failures use src/utils/ApiError.js, and successful API payloads use src/utils/ApiResponse.js. This gives controllers a consistent HTTP response contract rather than exposing raw database or upload errors.
Runtime and External Services
The service runs on Node.js with Express, uses MongoDB for persistence, Multer for multipart upload parsing, and Cloudinary for remote media storage. Runtime constants and environment-derived settings are centralized in src/constants.js; temporary upload files are intentionally excluded from version control under public/temp.
Main entry points: user.controller.js Account workflows · index.js Server startup · user.routes.js User routes · multer.middleware.js Multipart parser
Components 18
Runtime
- Process entry
index.js - Server startup
src/index.js - Runtime settings · Environment-derived runtime settings.
src/constants.js
HTTP API
- Express app
src/app.js - User routes
src/routes/user.routes.js - JWT authentication · Establishes authenticated user context for protected endpoints.
src/middlewares/auth.middleware.js - Async failure wrapper
src/utils/asyncHandler.js - API errors
src/utils/ApiError.js - API responses
src/utils/ApiResponse.js
Domain
- Account workflows · Registration, sessions, profiles, and image changes.
src/controllers/user.controller.js
Media pipeline
- Multipart parser · Writes incoming files to local temporary storage.
src/middlewares/multer.middleware.js - Temporary uploads
public/temp - Cloudinary hosting · Durable hosting boundary for uploaded media.
src/utils/cloudinary.js
Persistence
- MongoDB connection
src/db/index.js - MongoDB
- Users
src/models/user.models.js - Videos · Content model for planned or partial video features.
src/models/video.models.js - Subscriptions · Social graph model for planned or partial features.
src/models/subscription.model.js
Connections 19
- Process entry → Server startup: starts
- Server startup → Runtime settings: loads settings
- Server startup → MongoDB connection: initializes
- Server startup → Express app: runs
- Express app → User routes: mounts
- User routes → JWT authentication: protects endpoints with
- User routes → Multipart parser: parses uploads with
- JWT authentication → Account workflows: adds user context before
- Multipart parser → Temporary uploads: stores files in
- Multipart parser → Account workflows: passes files to
- Account workflows → Users: manages accounts
- Account workflows → Cloudinary hosting: uploads media through
- Account workflows → API responses: returns
- Async failure wrapper → Account workflows: wraps
- Account workflows → API errors: raises
- MongoDB connection → MongoDB: connects to
- Users → MongoDB: persists to
- Videos → MongoDB: persists to
- Subscriptions → MongoDB: persists to