# nguyenvinhtieng/server_social_media_vt- architecture

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

- Interactive diagram: https://gitdiagram.com/nguyenvinhtieng/server_social_media_vt-
- Repository: https://github.com/nguyenvinhtieng/server_social_media_vt-

## Overview

#### Purpose and Runtime

Node.js backend organized as an Express-style API for users, authentication, posts, comments, likes, and image uploads. The application starts from `src/index.js`, initializes database/cloud services, mounts routes, and integrates a socket layer.

#### Application Entry and Route Composition

`src/route/index.js` is the route aggregation boundary, delegating resource endpoints to:
- `src/route/auth.js`
- `src/route/user.js`
- `src/route/post.js`
- `src/route/comment.js`

Route modules should define HTTP-facing contracts and compose controllers with authorization, validation, and sanitization middleware.

#### Controllers: Request-to-Domain Orchestration

Controllers are the main application-service layer:
- `src/app/controllers/AuthController.js`
- `src/app/controllers/UserController.js`
- `src/app/controllers/PostController.js`
- `src/app/controllers/CommentController.js`

They translate authenticated HTTP requests into model operations, return API responses, and coordinate post/comment/user lifecycle behavior. Post and comment controllers are likely the main interaction path for social content and likes.

#### Persistence Model Boundary

Database entities are isolated in `src/app/models/`:
- `src/app/models/User.js`
- `src/app/models/Account.js`
- `src/app/models/Post.js`
- `src/app/models/Comment.js`
- `src/app/models/Like.js`

`User` and `Account` separate user/profile identity from account/authentication data. `Post`, `Comment`, and `Like` model the content graph. Database connection setup is centralized in `src/config/db.js`, keeping persistence infrastructure outside controllers and route declarations.

#### Security, Validation, and Authorization Pipeline

Middleware forms a request-policy boundary before controller execution:
- `src/app/middlewares/checkLogin.js`
- `src/app/middlewares/checkXss.js`
- `src/app/middlewares/validUserData.js`
- `src/app/middlewares/permissionAccessPost.js`
- `src/app/middlewares/permissionEditComment.js`
- `src/app/middlewares/permissionDeleteComment.js`
- `src/app/middlewares/permissionEditUser.js`

The expected flow is authentication first, input/XSS and user-data validation next, then ownership or access checks for protected mutations. `src/app/middlewares/userWasCreate.js` appears to protect account-creation state or prevent duplicate user setup.

#### External Services and Media Handling

Cloudinary configuration is isolated in `src/config/cloudinary.js`; image-transfer behavior is encapsulated in `src/utils/uploadImages.js`. Controllers should consume this utility rather than directly embedding Cloudinary calls, preserving a clear media-provider boundary. Sensitive values are expected through `src/credentials.js`.

#### Realtime Boundary

`src/socket/index.js` provides the socket integration point, likely attached during server bootstrap in `src/index.js`. It is the boundary for realtime notifications or content events and should remain separate from HTTP route handling while sharing domain/model operations where necessary.

Main entry points: [`src/app/middlewares/checkLogin.js`](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/middlewares/checkLogin.js) (Security & access middleware), [`src/index.js`](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/index.js) (Server bootstrap), [`src/route/index.js`](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/route/index.js) (Route aggregator), [`src/app/controllers/PostController.js`](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/controllers/PostController.js) (Post & comment controllers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_bootstrap["Bootstrap &amp; integrations"]
  node_server{{"Server bootstrap<br/>Node entrypoint<br/>[index.js]"}}
  node_db[("Database connection<br/>database setup<br/>[db.js]")]
  node_cloudinary["Cloudinary config<br/>media provider config<br/>[cloudinary.js]"]
  node_credentials["Credentials<br/>secrets module<br/>[credentials.js]"]
end

subgraph group_http["HTTP API"]
  node_routes["Route aggregator<br/>HTTP composition<br/>[index.js]"]
  node_auth_routes["Auth routes<br/>HTTP contracts<br/>[auth.js]"]
  node_user_routes["User routes<br/>HTTP contracts<br/>[user.js]"]
  node_content_routes["Post &amp; comment routes<br/>HTTP contracts<br/>[post.js]"]
  node_policy["Security &amp; access middleware<br/>request policy pipeline<br/>[checkLogin.js]"]
end

subgraph group_app["Application layer"]
  node_auth_controller["Auth controller<br/>application service<br/>[AuthController.js]"]
  node_user_controller["User controller<br/>application service<br/>[UserController.js]"]
  node_content_controllers["Post &amp; comment controllers<br/>application service<br/>[PostController.js]"]
  node_image_upload["Image upload utility<br/>media adapter<br/>[uploadImages.js]"]
end

subgraph group_data["Persistence"]
  node_identity_models[("User &amp; account models<br/>identity entities<br/>[User.js]")]
  node_content_models[("Post, comment &amp; like models<br/>content entities<br/>[Post.js]")]
end

subgraph group_realtime["Realtime"]
  node_socket["Socket integration<br/>realtime boundary<br/>[index.js]"]
end

node_server -->|"connects"| node_db
node_server -->|"initializes"| node_cloudinary
node_credentials -->|"configures"| node_cloudinary
node_server -->|"mounts"| node_routes
node_server -->|"attaches"| node_socket
node_routes -->|"delegates"| node_auth_routes
node_routes -->|"delegates"| node_user_routes
node_routes -->|"delegates"| node_content_routes
node_auth_routes -->|"applies"| node_policy
node_user_routes -->|"applies"| node_policy
node_content_routes -->|"applies"| node_policy
node_policy -->|"authorizes"| node_auth_controller
node_policy -->|"authorizes"| node_user_controller
node_policy -->|"authorizes"| node_content_controllers
node_auth_controller -->|"reads and writes"| node_identity_models
node_user_controller -->|"manages"| node_identity_models
node_content_controllers -->|"manages"| node_content_models
node_content_controllers -->|"resolves authors"| node_identity_models
node_content_controllers -->|"uploads images"| node_image_upload
node_image_upload -->|"uses"| node_cloudinary
node_socket -.->|"shares domain data"| node_content_models

click node_server "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/index.js"
click node_db "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/config/db.js"
click node_cloudinary "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/config/cloudinary.js"
click node_credentials "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/credentials.js"
click node_routes "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/route/index.js"
click node_auth_routes "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/route/auth.js"
click node_user_routes "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/route/user.js"
click node_content_routes "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/route/post.js"
click node_policy "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/middlewares/checkLogin.js"
click node_auth_controller "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/controllers/AuthController.js"
click node_user_controller "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/controllers/UserController.js"
click node_content_controllers "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/controllers/PostController.js"
click node_image_upload "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/utils/uploadImages.js"
click node_identity_models "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/models/User.js"
click node_content_models "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/models/Post.js"
click node_socket "https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/socket/index.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_server,node_db,node_cloudinary,node_credentials toneBlue
class node_routes,node_auth_routes,node_user_routes,node_content_routes,node_policy toneAmber
class node_auth_controller,node_user_controller,node_content_controllers,node_image_upload toneMint
class node_identity_models,node_content_models toneRose
class node_socket toneIndigo
```

## Components

### Bootstrap & integrations

- [Server bootstrap](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/index.js): Node entrypoint · Initializes services, HTTP routes, and sockets. · `src/index.js`
- [Database connection](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/config/db.js): database setup · `src/config/db.js`
- [Cloudinary config](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/config/cloudinary.js): media provider config · `src/config/cloudinary.js`
- [Credentials](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/credentials.js): secrets module · `src/credentials.js`

### HTTP API

- [Route aggregator](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/route/index.js): HTTP composition · `src/route/index.js`
- [Auth routes](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/route/auth.js): HTTP contracts · `src/route/auth.js`
- [User routes](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/route/user.js): HTTP contracts · `src/route/user.js`
- [Post & comment routes](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/route/post.js): HTTP contracts · `src/route/post.js`
- [Security & access middleware](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/middlewares/checkLogin.js): request policy pipeline · Authentication, validation, XSS filtering, and ownership checks. · `src/app/middlewares/checkLogin.js`

### Application layer

- [Auth controller](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/controllers/AuthController.js): application service · `src/app/controllers/AuthController.js`
- [User controller](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/controllers/UserController.js): application service · `src/app/controllers/UserController.js`
- [Post & comment controllers](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/controllers/PostController.js): application service · Coordinates content, comments, and likes. · `src/app/controllers/PostController.js`
- [Image upload utility](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/utils/uploadImages.js): media adapter · `src/utils/uploadImages.js`

### Persistence

- [User & account models](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/models/User.js): identity entities · Separates profile identity from authentication account data. · `src/app/models/User.js`
- [Post, comment & like models](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/app/models/Post.js): content entities · `src/app/models/Post.js`

### Realtime

- [Socket integration](https://github.com/nguyenvinhtieng/server_social_media_vt-/blob/main/src/socket/index.js): realtime boundary · `src/socket/index.js`

## Connections

- Server bootstrap → Database connection: connects
- Server bootstrap → Cloudinary config: initializes
- Credentials → Cloudinary config: configures
- Server bootstrap → Route aggregator: mounts
- Server bootstrap → Socket integration: attaches
- Route aggregator → Auth routes: delegates
- Route aggregator → User routes: delegates
- Route aggregator → Post & comment routes: delegates · Includes comment endpoints.
- Auth routes → Security & access middleware: applies
- User routes → Security & access middleware: applies
- Post & comment routes → Security & access middleware: applies
- Security & access middleware → Auth controller: authorizes
- Security & access middleware → User controller: authorizes
- Security & access middleware → Post & comment controllers: authorizes
- Auth controller → User & account models: reads and writes
- User controller → User & account models: manages
- Post & comment controllers → Post, comment & like models: manages
- Post & comment controllers → User & account models: resolves authors
- Post & comment controllers → Image upload utility: uploads images
- Image upload utility → Cloudinary config: uses
- Socket integration → Post, comment & like models: shares domain data
