Skip to main content

WWW Application Architecture

Application Structure

The www application follows a modular Express.js architecture with clear separation of concerns:

Core Components

1. Application Entry Point (app.js)

  • Initializes environment variables from Infisical
  • Sets up Express application with middleware
  • Configures Passport authentication
  • Starts HTTP server on configured port

2. Configuration Layer (src/config/)

  • EJS Configuration: Template engine setup
  • i18n Configuration: Internationalization for multiple languages
  • JWT Configuration: JSON Web Token handling
  • Passport Configuration: OAuth and authentication strategies

3. Middleware Layer (src/middleware/)

  • Authentication: User authentication checks
  • Language: Multi-language support (EN, ES, FR)
  • Route Management: Current route tracking
  • Site Maintenance: Site down functionality

4. Route Layer (src/routes/)

  • Account Routes: User account management
  • Auth Routes: Authentication and authorization
  • Public Routes: Public-facing pages
  • Helper Routes: Utility endpoints

5. Controller Layer (src/controllers/)

  • Auth Controllers: Handle authentication logic
  • Public Controllers: Manage public content (FAQs, countries)

6. Service Layer (src/services/)

  • Chart Service: Data visualization logic
  • Logger Service: Application logging with Winston
  • User Service: User management operations

7. View Layer (src/views/)

  • Layouts: Base templates and structure
  • Partials: Reusable template components
  • Account Pages: User account interfaces
  • Auth Pages: Login, registration, verification
  • Public Pages: Landing pages, resources

Data Flow

Request → Middleware → Routes → Controllers → Services → Views → Response

Request Processing Flow

  1. Incoming Request: HTTP request hits Express server
  2. Middleware Chain: Request passes through authentication, language, and utility middleware
  3. Route Matching: Express router matches URL to appropriate route handler
  4. Controller Logic: Route controller processes business logic
  5. Service Layer: Controllers call services for data operations
  6. View Rendering: EJS templates render HTML response
  7. Response: Processed response sent back to client

Key Architectural Patterns

1. MVC Pattern

  • Models: Data structures and business logic in services
  • Views: EJS templates for user interface
  • Controllers: Route handlers managing request/response flow

2. Middleware Pattern

  • Express middleware chain for cross-cutting concerns
  • Authentication, logging, internationalization

3. Module Pattern

  • ES6 modules with import/export
  • Custom import paths using package.json imports field

4. Configuration Pattern

  • Environment-based configuration
  • Centralized config management

Security Architecture

Authentication Flow

  1. User initiates login (Google OAuth or local)
  2. Passport.js handles authentication strategy
  3. JWT tokens issued for session management
  4. Middleware validates tokens on protected routes

Authorization

  • Role-based access control through middleware
  • Route-level protection for sensitive operations
  • User session management with Redis

Performance Considerations

Asset Optimization

  • SASS compilation with autoprefixer
  • JavaScript bundling with ESBuild
  • Asset minification with Terser

Caching Strategy

  • Redis for session storage
  • Static asset caching
  • Template caching in production

Build Process

  • Development: Hot reload with nodemon
  • Production: Optimized asset compilation
  • Source maps for debugging