How user1701/nodejs-shop-app works
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: app.ts App shell · controllers/shop.ts Shop controller · products.ts Products controller · routes/shop.ts Shop routes
Components 19
Express app
HTTP composition, routing, controllers, and middleware.
- App shell · Wires middleware, sessions, CSRF, static files, and route mounting.
src/app.ts - Shop routes · Storefront browsing, cart, checkout, orders, and invoices.
src/routes/shop.ts - Auth routes · Login, registration, password reset, and logout endpoints.
src/routes/auth.ts - Admin routes · Authenticated product management boundaries.
src/routes/admin.ts - Access guard · Protects authenticated and admin-only routes.
src/middleware/protectedRoutes.ts - Auth controller · Session-based authentication and password reset flow orchestration.
src/controllers/auth.ts - Shop controller · Browsing, cart, orders, checkout, and invoice access orchestration.
src/controllers/shop.ts - Products controller · Product CRUD and upload-driven edits.
src/controllers/products.ts - Common controller · Shared error and not-found handling.
src/controllers/common.ts
State & artifacts
Primary database models plus file-backed runtime artifacts.
- User model · Accounts and session-linked user state.
src/models/user.ts - Product model · Catalog items and uploaded image metadata.
src/models/product.ts - Order model · Completed purchases and invoice data.
src/models/order.ts - Uploads · Runtime product images served from public assets.
public/images - Invoices · Generated PDF invoice files stored for download.
- Static assets · Checked-in CSS and image assets served directly.
public/css/styles.css
Rendering layer
Server-side views and shared templates.
- Edge views · Server-rendered pages, layouts, and partials.
src/views - Layout · Shared application shell for rendered pages.
src/views/components/layout/app.edge
Connections 22
- 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.