How nikhil-dadhich/role-based-e-commerce works
Project type and purpose
• This is a full-stack, role-based e-commerce platform built on Node.js/Express, MongoDB, and EJS.
• It supports Admin and Customer roles, product & order management, secure authentication, CSRF protection, PDF invoice generation, and email-based password reset.
File structure
• Top-level:
• app.js – Entry point, configures Express, middleware, routes, error handling
• package.json / package-lock.json – Dependencies & scripts
• Directories:
• controllers/ – Business logic per resource (admin, auth, shop, error)
• routes/ – URL endpoints mapping to controllers (admin.js, auth.js, shop.js)
• models/ – Mongoose schemas (User, Product, Order)
• middleware/ – Auth guards (is-auth), CSRF, error handlers
• util/ – Helpers (file operations, path resolution, PDF generation, email sending)
• public/ – Static assets: CSS, client-side JS, images
• views/ – EJS templates organized by feature (admin, auth, shop) plus shared includes
• data/ – Seed data & generated invoices (PDFs)
• images/ – Uploaded product images
Key insights from README
• MVC pattern, modular code.
• Tech stack: Express, MongoDB/Mongoose, EJS templating, pdfkit, nodemailer, bcrypt, csurf.
• Features: role-based access, sessions, CSRF tokens, PDF invoices, password reset via email.
• Client (Browser) with UI (EJS pages, static CSS/JS)
• Express Web Server (app.js)
• Middleware Layer (CSRF, session, auth guards)
• Routing Layer (routes/*.js)
• Controller Layer (controllers/*.js)
• Data Access Layer (models/*.js via Mongoose)
• MongoDB Database
• File Storage (local disk or S3) for product images & invoice PDFs
• External Email Service (via nodemailer SMTP)
• Utilities (PDF generation with pdfkit)
b. Relationships & data flows
1. Browser → HTTP Request → Express
2. Express → pass through Middleware (session, CSRF, is-auth)
3. Routing Layer dispatches → Controller
4. Controller uses Models → read/write MongoDB
5. Controller → render EJS view or send JSON
6. Invoice flow: Controller → PDF util → local file system (data/invoices) → serve download
7. Email flow: Controller → nodemailer util → SMTP server → user inbox
8. Static assets served directly from public/
c. Architectural patterns & design principles
• MVC (Model-View-Controller)
• Single Express process handling all routes
• Middleware pipeline for crosscutting concerns (auth, CSRF, error handling)
• Role-based access control via middleware & user model flags
• Separation of concerns: util helpers abstract file and email logic
d. Technologies & frameworks
• Node.js / Express.js
• MongoDB with Mongoose ODM
• EJS templating engine
• pdfkit for PDF generation
• nodemailer for SMTP email
• bcrypt for password hashing
• csurf for CSRF tokens
• express-session for session management
• Break the system into at least these components: UI, Web Server, Middleware, Routing, Controllers, Models, DB, File Store, External Services.
• Session & CSRF checks in the middleware layer.
• Role-guard paths: Admin vs Customer routes.
• PDF generation and email dispatch as separate utility sub-blocks.