# ashwinn-si/bookreview-platform-mern-be architecture

> Architecture diagram of the GitHub repository ashwinn-si/bookreview-platform-mern-be, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ashwinn-si/bookreview-platform-mern-be
- Repository: https://github.com/ashwinn-si/bookreview-platform-mern-be

## Overview

##### Project Type and Purpose
- This is the **backend** of a full-stack MERN application.
- It exposes a JSON/REST API for book reviews, user management, authentication (JWT + Google OAuth), file uploads (Multer), and more.
- It follows an MVC (Model-View-Controller) layered architecture and interacts with a MongoDB database.

##### File Structure
- Top-level:
- server.js (entry point, DB connect + server start)
- package.json / lock (dependencies)
- README.md (docs)
- src/:
- config/ (dbConnect.js, passportConfig.js)
- app.js (Express app, middleware + routes wired here)
- route/ (API route definitions: auth, user, book, admin)
- controller/ (logic per resource: auth.js, user.js, book.js, admin.js)
- service/ (business logic layer invoked by controllers)
- model/ (Mongoose schemas: UserModel, bookModel, reviewModel, etc.)
- middleware/ (auth checks, role verification, JWT validation, request validators)
- utils/ (helpers: nodeMailer)

##### README for Additional Insights
- Tech stack: Node.js, Express, MongoDB/Mongoose, JWT, Passport, security middlewares
- MVC architecture explicitly mentioned
- Global error-handler, rate limiting, sanitization, XSS protection

##### Main Components & Their Interactions
- Client (Frontend SPA) → API Gateway (Express)
- Express Layer:
a. Global Middleware (security, body parsing, CORS, logging)
b. Route Handler (src/route/*)
c. Controller Layer (src/controller/*)
d. Service Layer (src/service/*)
e. Data Access Layer (src/model/* via Mongoose)
- External Services:
- MongoDB Atlas / local DB
- Google OAuth endpoints (passport-oauth strategy)
- SMTP provider (nodeMailer for OTP/email)
- Authentication: JWT issuance & verification, Passport for OAuth flows
- File Upload: Multer (for book images)

##### Architectural Patterns & Design Principles
- Layered MVC + Service abstraction (separation of concerns)
- Stateless authentication (JWT)
- Third-party integration via Passport (Google OAuth)
- Centralized configuration (src/config)
- Reusable middleware for validation, authorization, error handling

- Client (React front end)
- Web/API Tier (Express app)
- Business Tier (Controllers → Services)
- Data Tier (Mongoose models → MongoDB)
- External Integrations (OAuth / Email Service)
- Client → Express (HTTP requests)
- Express.router → Controller → Service → Model → MongoDB
- Service ←→ External services (OAuth, SMTP)
- Express.js, Helmet, XSS-Clean, rateLimiter
- JWT and Passport
- Multer
- Mongoose
- Green = API / Framework layer
- Blue = Business/Service layer
- Yellow = Data layer
- Orange = External systems/services

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client["React Frontend (SPA)"]:::external

    %% Server Initialization
    subgraph "Server Initialization"
        ServerJS["server.js"]:::api
        AppJS["app.js"]:::api
        DBConfig["dbConnect.js"]:::api
        PassportConfig["passportConfig.js"]:::api
    end

    %% Express Layer
    subgraph "API Tier (Express Layer)"
        Middleware["Global Middleware\n(Helmet, CORS, RateLimiter)"]:::api
        subgraph "Routing Layer"
            AuthRoute["auth.js"]:::api
            UserRoute["user.js"]:::api
            BookRoute["book.js"]:::api
            AdminRoute["admin.js"]:::api
        end
    end

    %% Business Layer
    subgraph "Business Tier"
        subgraph "Controllers"
            AuthController["auth.js"]:::service
            UserController["user.js"]:::service
            BookController["book.js"]:::service
            AdminController["admin.js"]:::service
        end
        subgraph "Services"
            AuthService["auth.js"]:::service
            UserService["user.js"]:::service
            BookService["book.js"]:::service
            AdminService["admin.js"]:::service
        end
    end

    %% Data Layer
    subgraph "Data Tier"
        subgraph "Models (Mongoose Schemas)"
            OTPModel["OtpModel.js"]:::data
            UserModel["UserModel.js"]:::data
            BookImageModel["bookImageModel.js"]:::data
            BookModel["bookModel.js"]:::data
            ContribBookModel["contributeBookModel.js"]:::data
            ReviewModel["reviewModel.js"]:::data
        end
        MongoDB["MongoDB Atlas / Local"]:::db
    end

    %% External Services
    subgraph "External Integrations"
        GoogleOAuth["Google OAuth"]:::external
        SMTP["SMTP Provider (nodeMailer)"]:::external
        FileStorage["File Upload (Multer)"]:::external
    end

    %% Flows
    Client -->|HTTP Requests| ServerJS
    ServerJS -->|import app| AppJS
    AppJS -->|init DB| DBConfig
    AppJS -->|init Passport| PassportConfig
    AppJS --> Middleware
    Middleware --> AuthRoute
    Middleware --> UserRoute
    Middleware --> BookRoute
    Middleware --> AdminRoute

    AuthRoute -->|calls| AuthController
    UserRoute -->|calls| UserController
    BookRoute -->|calls| BookController
    AdminRoute -->|calls| AdminController

    AuthController --> AuthService
    UserController --> UserService
    BookController --> BookService
    AdminController --> AdminService

    AuthService --> OTPModel
    AuthService --> UserModel
    AuthService -->|issues JWT| JWTVerification["jwtVerification.js"]:::api

    UserService --> UserModel
    BookService --> BookModel
    BookService --> BookImageModel
    BookService --> ContribBookModel
    BookService --> ReviewModel
    AdminService --> UserModel
    AdminService --> BookModel
    AdminService --> ReviewModel

    OTPModel --> MongoDB
    UserModel --> MongoDB
    BookImageModel --> MongoDB
    BookModel --> MongoDB
    ContribBookModel --> MongoDB
    ReviewModel --> MongoDB

    AuthService -->|OAuth Flow| GoogleOAuth
    AuthService -->|send Email/OTP| SMTP
    BookService -->|upload image| FileStorage

    %% Click Events
    click ServerJS "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/server.js"
    click AppJS "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/app.js"
    click DBConfig "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/config/dbConnect.js"
    click PassportConfig "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/config/passportConfig.js"
    click AuthRoute "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/route/auth.js"
    click UserRoute "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/route/user.js"
    click BookRoute "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/route/book.js"
    click AdminRoute "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/route/admin.js"
    click AuthController "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/controller/auth.js"
    click UserController "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/controller/user.js"
    click BookController "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/controller/book.js"
    click AdminController "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/controller/admin.js"
    click AuthService "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/service/auth.js"
    click UserService "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/service/user.js"
    click BookService "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/service/book.js"
    click AdminService "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/service/admin.js"
    click OTPModel "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/model/OtpModel.js"
    click UserModel "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/model/UserModel.js"
    click BookImageModel "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/model/bookImageModel.js"
    click BookModel "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/model/bookModel.js"
    click ContribBookModel "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/model/contributeBookModel.js"
    click ReviewModel "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/model/reviewModel.js"
    click JWTVerification "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/middleware/jwtVerification.js"
    click SMTP "https://github.com/ashwinn-si/bookreview-platform-mern-be/blob/master/src/utils/nodeMailer.js"

    %% Styles
    classDef api fill:#a8e6cf,stroke:#004d40,color:#004d40
    classDef service fill:#dcedc1,stroke:#33691e,color:#33691e
    classDef data fill:#fff9c4,stroke:#f57f17,color:#f57f17
    classDef db fill:#ffe0b2,stroke:#e65100,color:#e65100
    classDef external fill:#ffccbc,stroke:#bf360c,color:#bf360c
```
