How ananthu-kp/prodtrack works
ProdTrack is a single-node, monolithic web application:
• Backend: Node.js + Express REST API serving JSON and handling file uploads
• Frontend: Static HTML/CSS + vanilla JS (Fetch API) served from public/
• Database: MySQL (users + products tables)
• Storage: Local filesystem for uploaded product images (public/uploads/)
• Primary use case: Admin logs in → manages products (CRUD) → views dashboard analytics/report.
Runtime entry + composition
• app.js
The main process: Express app creation, middleware registration, static file hosting, route mounting, server listen.
Configuration / infrastructure
• config/db.js
MySQL connection pool (mysql2) + env wiring (dotenv).
HTTP routing layer
• routes/authRoutes.js
• routes/productRoutes.js
Express routers that define endpoints (/api/auth/login, /api/products/...).
Controller layer (HTTP → use-case)
• controllers/authController.js
• controllers/productController.js
Handles request parsing, response formatting, status codes, error mapping.
Service layer (business logic)
• services/authService.js
• services/productService.js
Encapsulates domain logic: credential verification, product CRUD, report aggregates, and likely image handling semantics (e.g., delete old image on update).
Middleware (cross-cutting)
• middleware/upload.js
Multer configuration: file size/type validation, storage destination, filename strategy.
Frontend (static)
• public/login.html, public/dashboard.html
• public/js/dashboard.js (Fetch calls to API)
• public/css/*.css
Database + ops
• database.sql (schema for users, products)
• scripts/create-admin.js (one-off admin bootstrap)
• package.json (dependencies: express, mysql2, bcrypt, multer, dotenv)
Public pages
• GET / (implicitly serves public/login.html if configured that way in app.js)
• public/dashboard.html is the authenticated UI.
• POST /api/auth/login
• GET /api/products
• GET /api/products/:id
• POST /api/products (multipart/form-data + image)
• PUT /api/products/:id (multipart/form-data)
• DELETE /api/products/:id
• GET /api/products/report
Data stores
• MySQL tables: users, products
• Upload dir: public/uploads/
• “Session management using localStorage” → represent as Browser LocalStorage storing auth/session token/flag (even if simplistic).
Login
• dashboard/login.html JS → POST /api/auth/login
• authRoutes → authController → authService
• authService → MySQL (users) + bcrypt verify
• Response → Browser stores “session” in LocalStorage → navigate to dashboard
Create/Update product with image
• Browser → POST/PUT /api/products... (multipart/form-data)
• productRoutes → upload middleware (multer) writes file to public/uploads
• productController → productService
• productService → MySQL (products insert/update)
• On update: productService deletes old image from filesystem (as README states)
Dashboard report
• Browser → GET /api/products/report
• productController → productService
• productService → MySQL aggregate queries
• Response renders totals/category breakdown in dashboard UI
HTTP paths (show direction + responsibility)
• public/js/dashboard.js calls /api/* endpoints (Fetch API)
• Express routes forward to controllers
• Controllers depend on services
• Services depend on:
• config/db.js pool for MySQL
• filesystem (public/uploads) for image lifecycle
• bcrypt (auth)
Data ownership
• MySQL is system of record for:
• users (credentials)
• product metadata (incl. image filename)
• filesystem stores:
• actual image bytes (named file)
• browser localStorage stores:
• auth/session indicator (as implemented)
Architectural patterns/principles to call out (and encode
• Layered architecture: Routes → Controllers → Services → DB
• Separation of concerns:
• Controllers: HTTP boundary
• Services: business rules + orchestration (DB + file cleanup)
• Middleware: cross-cutting request concerns (multer validation)
• Stateless backend (mostly):
• “Session in localStorage” implies backend isn’t maintaining server-side sessions; represent backend as stateless per-request (unless you see session middleware in app.js)
• Local disk storage for assets:
• Images are not in S3; they are in public/uploads
Client side
• Admin User
• Browser
• login.html
• dashboard.html
• dashboard.js
• LocalStorage (auth/session flag)
Server side (Node/Express)
• app.js (Express server)
• Static file server (express.static(public))
• authRoutes.js
• productRoutes.js
• authController.js
• productController.js
• authService.js (bcrypt)
• productService.js (CRUD + report + image cleanup)
• upload.js (multer: validation/storage)
• config/db.js (mysql2 pool)
Data stores
• MySQL prodtrack database
• users table
• products table
• Filesystem public/uploads/
Ops
• scripts/create-admin.js (one-off CLI → MySQL insert with bcrypt)