# user1701/nodejs-shop-app architecture

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

- Interactive diagram: https://gitdiagram.com/user1701/nodejs-shop-app
- Repository: https://github.com/user1701/nodejs-shop-app

## Overview

1. **Overall shape**
   This is a server-rendered e-commerce app built on **Node.js 20 + Express 5 + TypeScript**. The architecture is classic layered MVC-ish: `routes` define HTTP boundaries, `controllers` hold request logic, `models` encapsulate MongoDB persistence via Mongoose, and `views` render HTML with **Edge.js**.

2. **Application entry and composition**
   `src/app.ts` is the composition root. It wires global middleware, static asset serving, CSRF protection, sessions, flash messaging, and mounts the route modules. This is the main place where the app boundary is assembled before requests reach the domain code.

3. **HTTP route boundaries**
   The app is split into three user-facing route groups:
   - `src/routes/shop.ts` for storefront browsing, cart, and orders
   - `src/routes/auth.ts` for login, registration, password reset, logout
   - `src/routes/admin.ts` / product-related routes for authenticated product management  
   A middleware in `src/middleware/protectedRoutes.ts` gates the authenticated/admin-only paths.

4. **Controller layer**
   Controllers in `src/controllers/` are the main orchestration layer:
   - `auth.ts` handles session-based authentication flows
   - `shop.ts` handles browsing, cart, checkout, orders, and invoice access
   - `products.ts` handles CRUD for products
   - `common.ts` likely hosts shared/error/not-found behavior  
   Controllers bridge request data, validation, model access, and view rendering.

5. **Persistence and domain models**
   MongoDB is the primary database, accessed through Mongoose models in `src/models/`:
   - `user.ts` for accounts, sessions-related user state, and likely cart ownership
   - `product.ts` for catalog items and uploaded image metadata
   - `order.ts` for completed purchases and invoice data  
   This is the main durable state boundary for the app.

6. **Authentication and session state**
   Authentication is session-based, not token-based. The README indicates `express-session` with **MongoDB-backed session storage** via `connect-mongo`. That means login state survives restarts and is shared across instances if pointed at the same database. CSRF protection is enabled globally, so all mutating forms depend on per-request tokens.

7. **Shop/cart/order flow**
   The storefront flow is: browse products → add/remove items in cart → place order → view order history → download invoice PDF. Cart and order actions are authenticated, so the user/session boundary is central to the shopping flow. `src/controllers/shop.ts` and `src/models/order.ts` are the main components for this path.

8. **Product management and file uploads**
   Product creation/editing supports image uploads via **Multer**. Uploaded files are stored under `public/uploads` as referenced in the README, and utility helpers such as `getUploadedFilePath.ts` and `fs.ts` suggest the app also manages cleanup of old images when products change or are deleted. This creates a file-system side effect alongside MongoDB writes.

9. **Invoice generation**
   Orders can be exported as PDF invoices using **PDFKit**. The invoices are stored under `data/invoices/` and served back to the authenticated user. This is a separate artifact pipeline from the HTML views: the request may trigger PDF generation or retrieval instead of normal page rendering.

10. **Rendering layer**
   The UI is server-side rendered with **Edge.js** templates in `src/views/`. There is a shared layout under `views/components/layout`, plus page templates and reusable partials for forms, headers, product cards, pagination, and error messaging. This means the browser receives rendered HTML pages rather than a client-side SPA.

11. **Static assets and public surface**
   Static files live in `public/`, including CSS and images. Product images uploaded at runtime are also exposed from this static area. So the app has two file-serving concerns: checked-in assets and runtime-generated uploads.

12. **Shared configuration and utilities**
   `src/constants/` holds app-wide constants such as DB config, pagination, navigation, and user-related values. `src/utils/` contains infrastructure helpers for database connection, edge engine setup, paths, filesystem access, pagination, and upload-path derivation. These are the cross-cutting pieces that keep controllers and routes focused on business flow.

13. **Key external services**
   The architecture depends materially on:
   - **MongoDB** for primary persistence and session storage
   - **Mongoose** for data modeling
   - **Edge.js** for server-side HTML rendering
   - **Multer** for multipart uploads
   - **PDFKit** for invoices
   - **SendGrid**-related configuration for email/reset flows  
   These are not incidental libraries; they define the app’s storage, rendering, and delivery model.

14. **End-to-end request path**
   A typical request enters Express, passes global middleware such as session/CSRF/flash, then hits a route module, then a controller, which reads/writes Mongoose models or the file system, and finally renders an Edge template or returns a file response. That’s the core architecture to capture in a diagram: browser ⇄ Express middleware/routers ⇄ controllers ⇄ MongoDB/file system ⇄ rendered HTML/PDF response.

Main entry points: [`src/app.ts`](https://github.com/user1701/nodejs-shop-app/blob/main/src/app.ts) (App shell), [`src/controllers/shop.ts`](https://github.com/user1701/nodejs-shop-app/blob/main/src/controllers/shop.ts) (Shop controller), [`src/controllers/products.ts`](https://github.com/user1701/nodejs-shop-app/blob/main/src/controllers/products.ts) (Products controller), [`src/routes/shop.ts`](https://github.com/user1701/nodejs-shop-app/blob/main/src/routes/shop.ts) (Shop routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_app["Express app"]
  node_node_app["App shell<br/>Express composition root<br/>[app.ts]"]
  node_node_shop_routes["Shop routes<br/>HTTP routes<br/>[shop.ts]"]
  node_node_auth_routes["Auth routes<br/>HTTP routes<br/>[auth.ts]"]
  node_node_admin_routes["Admin routes<br/>HTTP routes<br/>[admin.ts]"]
  node_node_protected["Access guard<br/>middleware<br/>[protectedRoutes.ts]"]
  node_node_auth_controller["Auth controller<br/>[auth.ts]"]
  node_node_shop_controller["Shop controller<br/>[shop.ts]"]
  node_node_products_controller["Products controller<br/>[products.ts]"]
  node_node_common_controller["Common controller<br/>[common.ts]"]
end

subgraph group_group_data["State & artifacts"]
  node_node_user_model[("User model<br/>mongoose model<br/>[user.ts]")]
  node_node_product_model[("Product model<br/>mongoose model<br/>[product.ts]")]
  node_node_order_model[("Order model<br/>mongoose model<br/>[order.ts]")]
  node_node_uploads["Uploads<br/>filesystem artifact"]
  node_node_invoices["Invoices<br/>filesystem artifact"]
  node_node_public_css["Static assets<br/>[styles.css]"]
end

subgraph group_group_ui["Rendering layer"]
  node_node_edge_views["Edge views<br/>server templates"]
  node_node_layout["Layout<br/>[app.edge]"]
end

subgraph group_group_shared["Shared helpers"]
  node_node_constants["Constants<br/>shared config"]
  node_node_utils["Utils<br/>shared helpers"]
end

node_node_app -->|"mounts"| node_node_shop_routes
node_node_app -->|"mounts"| node_node_auth_routes
node_node_app -->|"mounts"| node_node_admin_routes
node_node_app -->|"renders"| node_node_edge_views
node_node_app -->|"serves"| node_node_public_css
node_node_shop_routes -->|"guards"| node_node_protected
node_node_admin_routes -->|"guards"| node_node_protected
node_node_shop_routes -->|"delegates"| node_node_shop_controller
node_node_auth_routes -->|"delegates"| node_node_auth_controller
node_node_admin_routes -->|"delegates"| node_node_products_controller
node_node_shop_controller -->|"reads/writes"| node_node_user_model
node_node_shop_controller -->|"reads"| node_node_product_model
node_node_shop_controller -->|"writes/reads"| node_node_order_model
node_node_products_controller -->|"writes/reads"| node_node_product_model
node_node_auth_controller -->|"reads/writes"| node_node_user_model
node_node_products_controller -->|"manages files"| node_node_uploads
node_node_shop_controller -->|"creates/serves"| node_node_invoices
node_node_edge_views -->|"uses"| node_node_layout
node_node_edge_views -->|"uses"| node_node_constants
node_node_app -->|"configures"| node_node_utils
node_node_products_controller -->|"uses"| node_node_utils
node_node_shop_controller -->|"uses"| node_node_utils

click node_node_app "https://github.com/user1701/nodejs-shop-app/blob/main/src/app.ts"
click node_node_shop_routes "https://github.com/user1701/nodejs-shop-app/blob/main/src/routes/shop.ts"
click node_node_auth_routes "https://github.com/user1701/nodejs-shop-app/blob/main/src/routes/auth.ts"
click node_node_admin_routes "https://github.com/user1701/nodejs-shop-app/blob/main/src/routes/admin.ts"
click node_node_protected "https://github.com/user1701/nodejs-shop-app/blob/main/src/middleware/protectedRoutes.ts"
click node_node_auth_controller "https://github.com/user1701/nodejs-shop-app/blob/main/src/controllers/auth.ts"
click node_node_shop_controller "https://github.com/user1701/nodejs-shop-app/blob/main/src/controllers/shop.ts"
click node_node_products_controller "https://github.com/user1701/nodejs-shop-app/blob/main/src/controllers/products.ts"
click node_node_common_controller "https://github.com/user1701/nodejs-shop-app/blob/main/src/controllers/common.ts"
click node_node_user_model "https://github.com/user1701/nodejs-shop-app/blob/main/src/models/user.ts"
click node_node_product_model "https://github.com/user1701/nodejs-shop-app/blob/main/src/models/product.ts"
click node_node_order_model "https://github.com/user1701/nodejs-shop-app/blob/main/src/models/order.ts"
click node_node_uploads "https://github.com/user1701/nodejs-shop-app/tree/main/public/images"
click node_node_edge_views "https://github.com/user1701/nodejs-shop-app/tree/main/src/views"
click node_node_layout "https://github.com/user1701/nodejs-shop-app/blob/main/src/views/components/layout/app.edge"
click node_node_public_css "https://github.com/user1701/nodejs-shop-app/blob/main/public/css/styles.css"
click node_node_constants "https://github.com/user1701/nodejs-shop-app/tree/main/src/constants"
click node_node_utils "https://github.com/user1701/nodejs-shop-app/tree/main/src/utils"

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_node_app,node_node_shop_routes,node_node_auth_routes,node_node_admin_routes,node_node_protected,node_node_auth_controller,node_node_shop_controller,node_node_products_controller,node_node_common_controller toneBlue
class node_node_user_model,node_node_product_model,node_node_order_model,node_node_uploads,node_node_invoices,node_node_public_css toneAmber
class node_node_edge_views,node_node_layout toneMint
class node_node_constants,node_node_utils toneRose
```

## Components

### Express app

HTTP composition, routing, controllers, and middleware.

- [App shell](https://github.com/user1701/nodejs-shop-app/blob/main/src/app.ts): Express composition root · Wires middleware, sessions, CSRF, static files, and route mounting. · `src/app.ts`
- [Shop routes](https://github.com/user1701/nodejs-shop-app/blob/main/src/routes/shop.ts): HTTP routes · Storefront browsing, cart, checkout, orders, and invoices. · `src/routes/shop.ts`
- [Auth routes](https://github.com/user1701/nodejs-shop-app/blob/main/src/routes/auth.ts): HTTP routes · Login, registration, password reset, and logout endpoints. · `src/routes/auth.ts`
- [Admin routes](https://github.com/user1701/nodejs-shop-app/blob/main/src/routes/admin.ts): HTTP routes · Authenticated product management boundaries. · `src/routes/admin.ts`
- [Access guard](https://github.com/user1701/nodejs-shop-app/blob/main/src/middleware/protectedRoutes.ts): middleware · Protects authenticated and admin-only routes. · `src/middleware/protectedRoutes.ts`
- [Auth controller](https://github.com/user1701/nodejs-shop-app/blob/main/src/controllers/auth.ts): controller · Session-based authentication and password reset flow orchestration. · `src/controllers/auth.ts`
- [Shop controller](https://github.com/user1701/nodejs-shop-app/blob/main/src/controllers/shop.ts): controller · Browsing, cart, orders, checkout, and invoice access orchestration. · `src/controllers/shop.ts`
- [Products controller](https://github.com/user1701/nodejs-shop-app/blob/main/src/controllers/products.ts): controller · Product CRUD and upload-driven edits. · `src/controllers/products.ts`
- [Common controller](https://github.com/user1701/nodejs-shop-app/blob/main/src/controllers/common.ts): controller · Shared error and not-found handling. · `src/controllers/common.ts`

### State & artifacts

Primary database models plus file-backed runtime artifacts.

- [User model](https://github.com/user1701/nodejs-shop-app/blob/main/src/models/user.ts): mongoose model · Accounts and session-linked user state. · `src/models/user.ts`
- [Product model](https://github.com/user1701/nodejs-shop-app/blob/main/src/models/product.ts): mongoose model · Catalog items and uploaded image metadata. · `src/models/product.ts`
- [Order model](https://github.com/user1701/nodejs-shop-app/blob/main/src/models/order.ts): mongoose model · Completed purchases and invoice data. · `src/models/order.ts`
- [Uploads](https://github.com/user1701/nodejs-shop-app/tree/main/public/images): filesystem artifact · Runtime product images served from public assets. · `public/images`
- **Invoices**: filesystem artifact · Generated PDF invoice files stored for download.
- [Static assets](https://github.com/user1701/nodejs-shop-app/blob/main/public/css/styles.css): static assets · Checked-in CSS and image assets served directly. · `public/css/styles.css`

### Rendering layer

Server-side views and shared templates.

- [Edge views](https://github.com/user1701/nodejs-shop-app/tree/main/src/views): server templates · Server-rendered pages, layouts, and partials. · `src/views`
- [Layout](https://github.com/user1701/nodejs-shop-app/blob/main/src/views/components/layout/app.edge): template layout · Shared application shell for rendered pages. · `src/views/components/layout/app.edge`

### Shared helpers

Cross-cutting constants and utilities used across the app.

- [Constants](https://github.com/user1701/nodejs-shop-app/tree/main/src/constants): shared config · App-wide navigation, pagination, database, and user constants. · `src/constants`
- [Utils](https://github.com/user1701/nodejs-shop-app/tree/main/src/utils): shared helpers · Database, edge engine, filesystem, path, pagination, and upload helpers. · `src/utils`

## Connections

- App shell → Shop routes: mounts · Registers storefront routes under Express.
- App shell → Auth routes: mounts · Registers authentication routes.
- App shell → Admin routes: mounts · Registers admin/product routes.
- App shell → Edge views: renders · Uses Edge templates for HTML responses.
- App shell → Static assets: serves · Exposes static assets and uploaded content.
- Shop routes → Access guard: guards · Requires authenticated access for sensitive storefront actions.
- Admin routes → Access guard: guards · Restricts product management to authorized users.
- Shop routes → Shop controller: delegates · Routes forward request handling to shop controller logic.
- Auth routes → Auth controller: delegates · Routes forward login and reset flows to controller logic.
- Admin routes → Products controller: delegates · Routes forward product CRUD to controller logic.
- Shop controller → User model: reads/writes · Uses user state for sessions, cart ownership, and auth context.
- Shop controller → Product model: reads · Loads products for browse and cart views.
- Shop controller → Order model: writes/reads · Creates orders and fetches order history/invoices.
- Products controller → Product model: writes/reads · Persists product catalog changes.
- Auth controller → User model: reads/writes · Authenticates users and updates password/session-related state.
- Products controller → Uploads: manages files · Stores and cleans up product images.
- Shop controller → Invoices: creates/serves · Generates and returns PDF invoices.
- Edge views → Layout: uses · Page templates share the application layout.
- Edge views → Constants: uses · Templates consume shared navigation and pagination data.
- App shell → Utils: configures · Bootstraps database, rendering, and filesystem helpers.
- Products controller → Utils: uses · Relies on upload path and filesystem helpers.
- Shop controller → Utils: uses · Relies on pagination, paths, and PDF/file helpers.
