# santoshkumar555/20-nodejs-virtual-library-api architecture

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

- Interactive diagram: https://gitdiagram.com/santoshkumar555/20-nodejs-virtual-library-api
- Repository: https://github.com/santoshkumar555/20-nodejs-virtual-library-api

## Overview

1. **Overall shape**
   This is a Node.js + Express REST API backed by MongoDB/Mongoose, with a small static frontend demo and Postman docs. The main runtime is the API server in `server.js`, which wires routes, middleware, database connectivity, and error handling.

2. **Primary domain boundaries**
   The code is split into three main feature areas:
   - `controllers/authors` for author CRUD
   - `controllers/books` for book CRUD
   - `controllers/userAccess` for authentication, profile, and admin user management  
   This separation is mirrored in `routes/authorRoutes.js`, `routes/bookRoutes.js`, and `routes/userAccessRoutes.js`.

3. **Request lifecycle**
   A typical request flows from:
   - route definition
   - optional middleware checks
   - controller logic
   - Mongoose model operations
   - response/error handling  
   The shared error boundary is `middleware/handleErrors.js`, which centralizes API error responses.

4. **Authentication and session model**
   User access is JWT-based, but login is OTP-driven rather than password-only. The user-access controllers cover:
   - registration
   - sending login OTP
   - verifying login OTP
   - logout
   - “get me” profile lookup
   - email checks
   - profile updates
   - forgot/reset password
   - self-deletion
   - admin create/delete user  
   `controllers/userAccess/generateToken.js` indicates token generation is factored out, and `middleware/isUserLoggedIn.js` likely protects authenticated endpoints.

5. **Authorization and role control**
   Access control is separated from authentication. `middleware/checkUserPermissions.js` suggests role- or permission-based enforcement for sensitive endpoints such as admin user actions and protected CRUD operations. This is an important architectural boundary: identity is verified first, then privileges are checked.

6. **Data model layer**
   The persistent domain is represented by three Mongoose models:
   - `models/User.js`
   - `models/Author.js`
   - `models/Book.js`  
   The README highlights author-book relationships via MongoDB ObjectIds, so books likely reference authors through IDs rather than embedded documents. This means the API has a relational-like dependency structure on top of MongoDB.

7. **Content management: authors and books**
   Authors and books are managed as independent CRUD resources, each with five standard endpoints:
   - list all
   - get by id
   - create
   - update
   - delete  
   The split into separate controllers per operation indicates a thin route layer and action-specific controller files rather than large monolithic controllers.

8. **File upload subsystem**
   `middleware/handleFileUpload.js` and `utils/deleteUploadedFiles.js` show that book or user-related image/file uploads are supported via Multer. The architecture includes cleanup on delete/update, so uploaded files are treated as external side effects that must stay consistent with database state.

9. **Email/OTP delivery**
   `config/emailService.js` and OTP login/forgot-password flows imply an outbound email subsystem. This service is part of the auth flow, not a standalone feature: it supports verification and recovery rather than general messaging.

10. **Configuration and environment**
   `config/database.js` centralizes database connection setup, while `.env.example` shows the app depends on environment variables for secrets and service settings. That makes the deployment boundary explicit: the API behavior changes based on runtime configuration rather than hardcoded values.

11. **Validation and defensive middleware**
   `utils/validateInput.js` suggests request payload validation is handled separately from business logic. Combined with upload handling, login checks, and permission checks, the API uses middleware to reject bad or unauthorized requests before controller logic runs.

12. **Frontend and documentation**
   The `frontend/` folder is a static client/demo served separately from the API logic, with `index.html`, `script.js`, `style.css`, and `_redirects`. The canonical developer-facing contract is documented through `docs/postman/virtual-library-api.postman.json`, and the README also points to Swagger UI in the live deployment.

13. **What to draw in an architecture diagram**
   A good diagram would show:
   - clients hitting the Express API
   - route modules dispatching to feature controllers
   - middleware gates for auth, permissions, uploads, and error handling
   - controllers reading/writing MongoDB via Mongoose models
   - auxiliary email service for OTP/password flows
   - filesystem/object storage side effect for uploads, with cleanup on deletion
   - a separate static frontend consuming the same API  
   The main trust boundaries are authentication/authorization, database persistence, and file/email side effects.

14. **Notable product intent**
   This is not just a CRUD library API: it is a secure library management backend with account recovery, OTP login, admin user management, and media upload lifecycle management. Those concerns are first-class in the architecture, not add-ons.

Main entry points: [`controllers/userAccess`](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/tree/main/controllers/userAccess) (User access), [`server.js`](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/server.js) (Server), [`routes/bookRoutes.js`](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/routes/bookRoutes.js) (Book routes), [`routes/authorRoutes.js`](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/routes/authorRoutes.js) (Author routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["API runtime"]
  node_server["Server<br/>Express app<br/>[server.js]"]
  node_author_routes["Author routes<br/>route module<br/>[authorRoutes.js]"]
  node_book_routes["Book routes<br/>route module<br/>[bookRoutes.js]"]
  node_user_routes["User routes<br/>route module"]
  node_authz_mw["Access guard<br/>middleware<br/>[isUserLoggedIn.js]"]
  node_perm_mw["Perms check<br/>middleware"]
  node_upload_mw["Upload handler<br/>middleware"]
  node_error_mw["Error handler<br/>middleware<br/>[handleErrors.js]"]
end

subgraph group_domain["Domain"]
  node_author_ctrl["Authors<br/>controllers"]
  node_book_ctrl["Books<br/>controllers"]
  node_user_ctrl["User access<br/>controllers"]
  node_token_util["Token util<br/>auth helper<br/>[generateToken.js]"]
end

subgraph group_persistence["Persistence"]
  node_user_model[("User model<br/>mongoose model<br/>[User.js]")]
  node_author_model[("Author model<br/>mongoose model<br/>[Author.js]")]
  node_book_model[("Book model<br/>mongoose model<br/>[Book.js]")]
  node_file_util["File cleanup"]
end

subgraph group_support["Support"]
  node_db_config["DB config<br/>[database.js]"]
  node_email_service["Email service<br/>[emailService.js]"]
  node_validate_util["Validate input<br/>[validateInput.js]"]
end

subgraph group_client["Client & docs"]
  node_frontend["Frontend<br/>static client<br/>[index.html]"]
  node_postman_docs["Postman docs<br/>api docs"]
end

node_server -->|"connects"| node_db_config
node_server -->|"mounts"| node_author_routes
node_server -->|"mounts"| node_book_routes
node_server -->|"mounts"| node_user_routes
node_server -->|"uses"| node_error_mw
node_author_routes -->|"protects"| node_authz_mw
node_author_routes -->|"guards"| node_perm_mw
node_author_routes -->|"dispatches"| node_author_ctrl
node_book_routes -->|"protects"| node_authz_mw
node_book_routes -->|"guards"| node_perm_mw
node_book_routes -->|"accepts"| node_upload_mw
node_book_routes -->|"dispatches"| node_book_ctrl
node_user_routes -->|"dispatches"| node_user_ctrl
node_user_ctrl -->|"generates"| node_token_util
node_user_ctrl -->|"sends"| node_email_service
node_user_ctrl -->|"validates"| node_validate_util
node_user_ctrl -->|"requires"| node_authz_mw
node_user_ctrl -->|"requires"| node_perm_mw
node_author_ctrl -->|"reads/writes"| node_author_model
node_book_ctrl -->|"reads/writes"| node_book_model
node_book_ctrl -.->|"references"| node_author_model
node_user_ctrl -->|"reads/writes"| node_user_model
node_book_ctrl -->|"cleans up"| node_file_util
node_upload_mw -.->|"pairs with"| node_file_util
node_frontend -->|"consumes"| node_server
node_postman_docs -.->|"describes"| node_server

click node_server "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/server.js"
click node_author_routes "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/routes/authorRoutes.js"
click node_book_routes "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/routes/bookRoutes.js"
click node_user_routes "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/routes/userAccessRoutes.js"
click node_authz_mw "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/middleware/isUserLoggedIn.js"
click node_perm_mw "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/middleware/checkUserPermissions.js"
click node_upload_mw "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/middleware/handleFileUpload.js"
click node_error_mw "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/middleware/handleErrors.js"
click node_author_ctrl "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/tree/main/controllers/authors"
click node_book_ctrl "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/tree/main/controllers/books"
click node_user_ctrl "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/tree/main/controllers/userAccess"
click node_token_util "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/controllers/userAccess/generateToken.js"
click node_user_model "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/models/User.js"
click node_author_model "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/models/Author.js"
click node_book_model "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/models/Book.js"
click node_file_util "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/utils/deleteUploadedFiles.js"
click node_db_config "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/config/database.js"
click node_email_service "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/config/emailService.js"
click node_validate_util "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/utils/validateInput.js"
click node_frontend "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/frontend/index.html"
click node_postman_docs "https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/docs/postman/virtual-library-api.postman.json"

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_author_routes,node_book_routes,node_user_routes,node_authz_mw,node_perm_mw,node_upload_mw,node_error_mw toneBlue
class node_author_ctrl,node_book_ctrl,node_user_ctrl,node_token_util toneAmber
class node_user_model,node_author_model,node_book_model,node_file_util toneMint
class node_db_config,node_email_service,node_validate_util toneRose
class node_frontend,node_postman_docs toneIndigo
```

## Components

### API runtime

Express server, routes, controllers, and middleware

- [Server](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/server.js): Express app · Bootstraps the API, middleware, routes, and shutdown/error wiring · `server.js`
- [Author routes](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/routes/authorRoutes.js): route module · Endpoints for author CRUD · `routes/authorRoutes.js`
- [Book routes](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/routes/bookRoutes.js): route module · Endpoints for book CRUD · `routes/bookRoutes.js`
- [User routes](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/routes/userAccessRoutes.js): route module · Endpoints for registration, login, profile, and admin actions · `routes/userAccessRoutes.js`
- [Access guard](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/middleware/isUserLoggedIn.js): middleware · Checks login state and permissions · `middleware/isUserLoggedIn.js`
- [Perms check](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/middleware/checkUserPermissions.js): middleware · Role or permission enforcement for protected actions · `middleware/checkUserPermissions.js`
- [Upload handler](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/middleware/handleFileUpload.js): middleware · Handles multipart uploads and file intake · `middleware/handleFileUpload.js`
- [Error handler](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/middleware/handleErrors.js): middleware · Central API error responses · `middleware/handleErrors.js`

### Domain

Author, book, and user access features

- [Authors](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/tree/main/controllers/authors): controllers · Author CRUD actions · `controllers/authors`
- [Books](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/tree/main/controllers/books): controllers · Book CRUD actions · `controllers/books`
- [User access](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/tree/main/controllers/userAccess): controllers · Auth, profile, password, and admin user flows · `controllers/userAccess`
- [Token util](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/controllers/userAccess/generateToken.js): auth helper · Generates JWTs for authenticated sessions · `controllers/userAccess/generateToken.js`

### Persistence

MongoDB models and file side effects

- [User model](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/models/User.js): mongoose model · User persistence and identity data · `models/User.js`
- [Author model](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/models/Author.js): mongoose model · Author persistence · `models/Author.js`
- [Book model](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/models/Book.js): mongoose model · Book persistence and author references · `models/Book.js`
- [File cleanup](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/utils/deleteUploadedFiles.js): utility · Deletes uploaded files during update/delete flows · `utils/deleteUploadedFiles.js`

### Support

Config, email, validation, and shared utilities

- [DB config](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/config/database.js): config · MongoDB connection setup · `config/database.js`
- [Email service](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/config/emailService.js): service · Sends OTP and password recovery email flows · `config/emailService.js`
- [Validate input](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/utils/validateInput.js): utility · Request payload validation helpers · `utils/validateInput.js`

### Client & docs

Static frontend and API documentation

- [Frontend](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/frontend/index.html): static client · Small static demo client consuming the API · `frontend/index.html`
- [Postman docs](https://github.com/santoshkumar555/20-nodejs-virtual-library-api/blob/main/docs/postman/virtual-library-api.postman.json): api docs · Canonical request collection for the API · `docs/postman/virtual-library-api.postman.json`

## Connections

- Server → DB config: connects · Initializes the database connection
- Server → Author routes: mounts · Registers author endpoints
- Server → Book routes: mounts · Registers book endpoints
- Server → User routes: mounts · Registers user-access endpoints
- Server → Error handler: uses · Centralizes API error handling
- Author routes → Access guard: protects · Applies login checks where needed
- Author routes → Perms check: guards · Applies privilege checks for sensitive operations
- Author routes → Authors: dispatches · Routes CRUD requests to author controllers
- Book routes → Access guard: protects · Applies login checks where needed
- Book routes → Perms check: guards · Applies privilege checks for sensitive operations
- Book routes → Upload handler: accepts · Handles file uploads on book workflows
- Book routes → Books: dispatches · Routes CRUD requests to book controllers
- User routes → User access: dispatches · Routes auth and profile requests
- User access → Token util: generates · Creates JWTs after successful auth
- User access → Email service: sends · Delivers login OTP and recovery email flows
- User access → Validate input: validates · Checks input before state changes
- User access → Access guard: requires · Uses login state for protected user actions
- User access → Perms check: requires · Uses permissions for admin actions
- Authors → Author model: reads/writes · Persists author records
- Books → Book model: reads/writes · Persists book records
- Books → Author model: references · Books are linked to authors by id
- User access → User model: reads/writes · Persists user records
- Books → File cleanup: cleans up · Removes uploaded files when records change
- Upload handler → File cleanup: pairs with · Supports upload lifecycle cleanup
- Frontend → Server: consumes · Static client talks to the API
- Postman docs → Server: describes · Collection documents API behavior
