# prathamesh3245/js-backend architecture

> Architecture diagram of the GitHub repository prathamesh3245/js-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/prathamesh3245/js-backend
- Repository: https://github.com/prathamesh3245/js-backend

## Overview

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: [`src/controllers/user.controller.js`](https://github.com/prathamesh3245/js-backend/blob/main/src/controllers/user.controller.js) (User controller), [`src/routes/user.routes.js`](https://github.com/prathamesh3245/js-backend/blob/main/src/routes/user.routes.js) (User routes), [`src/app.js`](https://github.com/prathamesh3245/js-backend/blob/main/src/app.js) (Express app), [`src/utils/cloudinary.js`](https://github.com/prathamesh3245/js-backend/blob/main/src/utils/cloudinary.js) (Cloudinary util)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_src_index["Entry point<br/>bootstrap<br/>[index.js]"]
  node_src_constants["Constants<br/>[constants.js]"]
end

subgraph group_http["HTTP API"]
  node_src_app["Express app<br/>http composition<br/>[app.js]"]
  node_src_routes_user["User routes<br/>route map<br/>[user.routes.js]"]
  node_src_mw_auth["Auth guard<br/>jwt middleware<br/>[auth.middleware.js]"]
  node_src_mw_multer["Upload gate<br/>multipart middleware"]
end

subgraph group_domain["Domain"]
  node_src_ctrl_user["User controller<br/>api controller<br/>[user.controller.js]"]
  node_src_models_user[("User model<br/>mongoose model<br/>[user.model.js]")]
  node_src_models_video[("Video model<br/>mongoose model<br/>[video.model.js]")]
  node_src_models_subscription[("Subscription model<br/>mongoose model")]
end

subgraph group_integrations["Integrations"]
  node_src_db_index["DB connector<br/>mongo bootstrap<br/>[index.js]"]
  node_mongo[("MongoDB<br/>database")]
  node_cloudinary(("Cloudinary<br/>media store"))
end

subgraph group_utils["Utilities"]
  node_src_utils_cloudinary["Cloudinary util<br/>upload helper<br/>[cloudinary.js]"]
  node_src_utils_apierror["ApiError<br/>error helper<br/>[ApiError.js]"]
  node_src_utils_apiresponse["ApiResponse<br/>response helper<br/>[ApiResponse.js]"]
  node_src_utils_asynchandler["Async handler<br/>promise helper<br/>[asyncHandler.js]"]
end

node_src_index -->|"boots"| node_src_app
node_src_index -->|"connects"| node_src_db_index
node_src_db_index -->|"uses"| node_mongo
node_src_app -->|"mounts"| node_src_routes_user
node_src_routes_user -->|"protects"| node_src_mw_auth
node_src_routes_user -->|"parses uploads"| node_src_mw_multer
node_src_routes_user -->|"dispatches"| node_src_ctrl_user
node_src_ctrl_user -->|"wrapped by"| node_src_utils_asynchandler
node_src_ctrl_user -->|"returns"| node_src_utils_apiresponse
node_src_ctrl_user -->|"throws"| node_src_utils_apierror
node_src_ctrl_user -->|"reads/writes"| node_src_models_user
node_src_ctrl_user -->|"reads"| node_src_models_video
node_src_ctrl_user -->|"reads"| node_src_models_subscription
node_src_ctrl_user -->|"uploads media"| node_src_utils_cloudinary
node_src_utils_cloudinary -->|"stores"| node_cloudinary
node_src_mw_multer -->|"prepares files"| node_src_utils_cloudinary
node_src_app -->|"configures"| node_src_constants
node_src_ctrl_user -.->|"depends on"| node_src_mw_auth
node_src_ctrl_user -.->|"persists via models"| node_mongo

click node_src_index "https://github.com/prathamesh3245/js-backend/blob/main/src/index.js"
click node_src_app "https://github.com/prathamesh3245/js-backend/blob/main/src/app.js"
click node_src_routes_user "https://github.com/prathamesh3245/js-backend/blob/main/src/routes/user.routes.js"
click node_src_mw_auth "https://github.com/prathamesh3245/js-backend/blob/main/src/middlewares/auth.middleware.js"
click node_src_mw_multer "https://github.com/prathamesh3245/js-backend/blob/main/src/middlewares/multer.middleware.js"
click node_src_ctrl_user "https://github.com/prathamesh3245/js-backend/blob/main/src/controllers/user.controller.js"
click node_src_models_user "https://github.com/prathamesh3245/js-backend/blob/main/src/models/user.model.js"
click node_src_models_video "https://github.com/prathamesh3245/js-backend/blob/main/src/models/video.model.js"
click node_src_models_subscription "https://github.com/prathamesh3245/js-backend/blob/main/src/models/subscription.models.js"
click node_src_db_index "https://github.com/prathamesh3245/js-backend/blob/main/src/db/index.js"
click node_src_utils_cloudinary "https://github.com/prathamesh3245/js-backend/blob/main/src/utils/cloudinary.js"
click node_src_utils_apierror "https://github.com/prathamesh3245/js-backend/blob/main/src/utils/ApiError.js"
click node_src_utils_apiresponse "https://github.com/prathamesh3245/js-backend/blob/main/src/utils/ApiResponse.js"
click node_src_utils_asynchandler "https://github.com/prathamesh3245/js-backend/blob/main/src/utils/asyncHandler.js"
click node_src_constants "https://github.com/prathamesh3245/js-backend/blob/main/src/constants.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_index,node_src_constants toneBlue
class node_src_app,node_src_routes_user,node_src_mw_auth,node_src_mw_multer toneAmber
class node_src_ctrl_user,node_src_models_user,node_src_models_video,node_src_models_subscription toneMint
class node_src_db_index,node_mongo,node_cloudinary toneRose
class node_src_utils_cloudinary,node_src_utils_apierror,node_src_utils_apiresponse,node_src_utils_asynchandler toneIndigo
```

## Components

### Runtime

Process bootstrap and HTTP app startup

- [Entry point](https://github.com/prathamesh3245/js-backend/blob/main/src/index.js): bootstrap · Starts the server, loads config, and connects the app to MongoDB · `src/index.js`
- [Constants](https://github.com/prathamesh3245/js-backend/blob/main/src/constants.js): config constants · Shared application constants · `src/constants.js`

### HTTP API

Express app, routes, and request middleware

- [Express app](https://github.com/prathamesh3245/js-backend/blob/main/src/app.js): http composition · Configures global middleware and mounts versioned routes · `src/app.js`
- [User routes](https://github.com/prathamesh3245/js-backend/blob/main/src/routes/user.routes.js): route map · Defines the user/account API surface under /api/v1 · `src/routes/user.routes.js`
- [Auth guard](https://github.com/prathamesh3245/js-backend/blob/main/src/middlewares/auth.middleware.js): jwt middleware · Validates access tokens from cookies or headers and sets user context · `src/middlewares/auth.middleware.js`
- [Upload gate](https://github.com/prathamesh3245/js-backend/blob/main/src/middlewares/multer.middleware.js): multipart middleware · Parses multipart requests and stages files in public/temp · `src/middlewares/multer.middleware.js`

### Domain

Controllers and persistence models

- [User controller](https://github.com/prathamesh3245/js-backend/blob/main/src/controllers/user.controller.js): api controller · Orchestrates registration, login, profile, media, and history workflows · `src/controllers/user.controller.js`
- [User model](https://github.com/prathamesh3245/js-backend/blob/main/src/models/user.model.js): mongoose model · Stores account, profile, auth, and channel state · `src/models/user.model.js`
- [Video model](https://github.com/prathamesh3245/js-backend/blob/main/src/models/video.model.js): mongoose model · Represents uploaded videos and related metadata · `src/models/video.model.js`
- [Subscription model](https://github.com/prathamesh3245/js-backend/blob/main/src/models/subscription.models.js): mongoose model · Captures user-to-user subscription relationships · `src/models/subscription.models.js`

### Integrations

External storage and database boundaries

- [DB connector](https://github.com/prathamesh3245/js-backend/blob/main/src/db/index.js): mongo bootstrap · Initializes the MongoDB connection · `src/db/index.js`
- **MongoDB**: database · Primary persistence store for users, videos, and subscriptions
- **Cloudinary**: media store · Persistent storage for avatars, cover images, and other uploads

### Utilities

Shared response, error, and upload helpers

- [Cloudinary util](https://github.com/prathamesh3245/js-backend/blob/main/src/utils/cloudinary.js): upload helper · Wraps media upload operations to Cloudinary · `src/utils/cloudinary.js`
- [ApiError](https://github.com/prathamesh3245/js-backend/blob/main/src/utils/ApiError.js): error helper · Standard application error wrapper · `src/utils/ApiError.js`
- [ApiResponse](https://github.com/prathamesh3245/js-backend/blob/main/src/utils/ApiResponse.js): response helper · Standard success response wrapper · `src/utils/ApiResponse.js`
- [Async handler](https://github.com/prathamesh3245/js-backend/blob/main/src/utils/asyncHandler.js): promise helper · Centralizes async route error propagation · `src/utils/asyncHandler.js`

## Connections

- 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
