Overview
The EPR LAPS Backend API is built on Hapi.js, a robust Node.js framework designed for building scalable applications. The system follows a plugin-based architecture that promotes modularity, separation of concerns, and testability.Core Architecture Pattern
The application uses the Hapi Plugin Architecture to organize functionality into discrete, testable modules. Each plugin registers itself with the server and can extend the request lifecycle.Server Initialization
The server is initialized insrc/server.js:15-66 with the following plugins registered in order:
Plugin registration order matters. Authentication must be registered before authorization, and both must be registered before the router.
Hapi Plugin Architecture
Plugin Structure
Every Hapi plugin follows a standard structure:Request Lifecycle Hooks
Plugins can tap into Hapi’s request lifecycle using extension points:Request Lifecycle Extension Points
Request Lifecycle Extension Points
- onRequest - Called at the beginning of the request, before authentication
- onPreAuth - Before authentication is performed
- onPostAuth - After authentication, ideal for authorization checks
- onPreHandler - Before the route handler is called
- onPostHandler - After the route handler completes
- onPreResponse - Before the response is sent to the client
MongoDB Integration
The MongoDB plugin (src/common/helpers/mongodb.js) provides database connectivity and distributed locking capabilities.
Connection Management
The MongoDB plugin establishes a connection pool at server startup:Server and Request Decoration
The plugin decorates both the server and request objects for easy access:Request decorations use
apply: true, which means they’re functions that return the value when called.Distributed Locking
The system usesmongo-locks for distributed locking to prevent race conditions in concurrent operations:
id field for optimal performance:
Graceful Shutdown
The MongoDB plugin registers a shutdown handler to cleanly close connections:Configuration Management
The application uses Convict for schema-based configuration with environment variable support:Security Features
The server is configured with security best practices (src/server.js:20-36):
Request Flow
- Request Received - Server accepts incoming HTTP request
- Request Tracing - Trace ID extracted or generated
- Request Logging - Request details logged
- Authentication - JWT token validated (see Authentication)
- Authorization - Role-based access control applied (see Authorization)
- Validation - Request payload validated against schema
- Route Handler - Business logic executed
- Response - Response sent to client
Environment Support
The application supports multiple environments:local- Local developmentdev- Development environmenttest- Testing environmentperf-test- Performance testingext-test- External testingprod- Production environment
The environment is controlled via the
ENVIRONMENT environment variable and affects logging, metrics, and security settings.