System Architecture
PaparcApp follows a Model-View-Controller (MVC) architecture pattern built on top of Express.js, a minimal and flexible Node.js web application framework.Architecture Diagram
Express.js Configuration
The main application entry point isapp.js, which configures the Express server:
Core Configuration
Middleware Pipeline
Middleware is executed in order for every request:- logger(‘dev’): HTTP request logging using Morgan
- express.json(): Parses incoming JSON payloads
- express.urlencoded(): Parses URL-encoded form data
- cookieParser(): Parses Cookie header and populates
req.cookies - express.static(): Serves static assets (CSS, JS, images) from
public/ - session(): Maintains user sessions with server-side storage
- authLocalsMiddleware: Injects authentication state into response locals
Session Configuration
- Sessions persist user authentication across requests
- Session data stored server-side with client cookie identifier
- Secure cookies in production (HTTPS required)
- Trust proxy setting for cloud deployment (Render.com)
Routing System
PaparcApp uses a modular routing approach with four main routers:Route Structure
1. Public Routes (/)
routes/index.jsController:
mainController.jsPurpose: Public-facing pages for customers
2. Authentication Routes (/users)
routes/users.jsController:
authController.jsPurpose: User authentication and account management
3. Admin Routes (/admin) - Protected
routes/admin.jsController:
adminController.jsMiddleware:
isAdmin (blocks non-admin users)Purpose: Administrative dashboard for managing reservations
4. API Routes (/api)
routes/api.jsController:
apiController.jsPurpose: RESTful API for AJAX requests and public booking
Authentication Middleware
PaparcApp implements three authentication middleware functions:1. authLocalsMiddleware
res.locals for EJS templatesLocation:
middlewares/auth.jsUsage: Applied globally to all routes
Variables exposed:
isLoggedIn: Boolean indicating if user is authenticateduser: User object with id, name, email, roleisAdmin: Boolean indicating if user has ADMIN role
2. isLoggedIn
Behavior: Redirects to login page if not authenticated
Usage: Apply to protected routes (e.g., user profile)
3. isAdmin
Behavior: Redirects to home page if not admin
Usage: Applied to all
/admin/* routes
Services Layer
PricingService (Singleton Pattern)
The PricingService is a singleton class that maintains an in-memory cache of pricing data:- Pricing data is static and rarely changes
- Loading from database on every request would be inefficient
- In-memory cache provides instant access for price calculations
- Single initialization at server startup
app.js:89):
Model Layer (DAO Pattern)
PaparcApp uses the Data Access Object (DAO) pattern to separate database logic from business logic:DAO Architecture
- Centralized database queries
- Easy to test and mock
- Database abstraction (could switch from PostgreSQL to MySQL)
- Reusable data access methods
Available DAOs
- customer-dao.js: Customer CRUD operations and vehicle associations
- vehicle-dao.js: Vehicle management and lookups
- reservation-dao.js: Reservation lifecycle (create, update, cancel, finalize)
- pricing-dao.js: Load pricing tables (coefficients, rates, extras)
- service-catalog-dao.js: Service catalog queries (main services, additional services, plans)
Example DAO Implementation
new PricingDAO())
Database Connection
PostgreSQL Connection Pool
- Reuses database connections instead of opening/closing for each query
- Reduces connection overhead
- Handles concurrent requests efficiently
- Automatic connection management
Error Handling
404 Handler
Global Error Handler
- Catches all unhandled errors
- Shows full error stack in development
- Hides error details in production (security)
- Renders custom error page
File Structure
Key Design Patterns
1. MVC (Model-View-Controller)
- Model: DAOs handle data access
- View: EJS templates render HTML
- Controller: Controllers handle request logic
2. Singleton Pattern
- PricingService: Single instance with in-memory cache
- DAOs: Exported as single instances
- Database Pool: Single pool for all connections
3. Middleware Chain
- Sequential request processing
- Reusable cross-cutting concerns (logging, auth, parsing)
4. Repository Pattern (DAO)
- Abstracts database operations
- Provides clean interface for data access
Summary
PaparcApp is a well-structured Express.js application that leverages:- MVC architecture for clear separation of concerns
- Middleware pipeline for request processing
- DAO pattern for database abstraction
- Singleton services for performance optimization
- Session-based authentication with role-based access control
- Modular routing for maintainable code organization
