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
- Incoming Request: HTTP request hits Express server
- Middleware Chain: Request passes through authentication, language, and utility middleware
- Route Matching: Express router matches URL to appropriate route handler
- Controller Logic: Route controller processes business logic
- Service Layer: Controllers call services for data operations
- View Rendering: EJS templates render HTML response
- 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
- User initiates login (Google OAuth or local)
- Passport.js handles authentication strategy
- JWT tokens issued for session management
- 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