audits folder.
Overview
This report analyzes design patterns in the Express Login Demo application, evaluates correctness and appropriateness, and identifies actionable improvements that increase maintainability, testability, and security. Project highlights:- Stack: Express.js, PostgreSQL (pg), bcrypt, jsonwebtoken, express-validator
- Authentication: JWT (access + refresh pattern)
- Files of note:
server.js,routes/auth.js,config/database.js - Database: PostgreSQL connection pool (singleton)
Project Structure (summary)
- Simple Express REST application
- Auth routes currently contain SQL and token logic inline
- Database access uses
pgPool (connection pooling)
1. CREATIONAL PATTERNS
1.1 Singleton Pattern
Found: PostgreSQL connection poolLocation:
config/database.js:3
- Correct: single Pool instance exported and reused across modules.
- Appropriate: DB pools should be application-scoped.
- Tip: Ensure connect/error listeners log useful diagnostics and exit gracefully when necessary.
1.2 Factory Pattern
Status: Not present (recommended) Rationale:- Token creation is currently inline in controllers. A
TokenFactorycentralizes JWT creation, expiration policies, and secret usage, which simplifies secret rotation and testing.
1.3 Builder Pattern
Status: Not present / Not necessary Assessment:- Validation chains from
express-validatoralready provide a builder-like API. - No complex object construction that warrants a Builder pattern in the current scope.
2. STRUCTURAL PATTERNS
2.1 Facade Pattern
Found: Express RouterLocation:
routes/auth.js
- Router is correctly used as a facade to group auth endpoints and hide implementation details.
2.2 Decorator / Middleware Pattern
Found: Express middleware chain and validation middleware- Middleware separation (validation, parsing) is well applied.
- Missing: a reusable JWT authentication middleware applied to protected routes.
middleware/ and apply to endpoints):
2.3 Proxy Pattern (Query Caching)
Status: Not implemented (recommended) Assessment:- No caching layer for frequent read queries.
- A lightweight DB proxy with TTL-based in-memory caching reduces load for read-heavy endpoints.
3. BEHAVIORAL PATTERNS
3.1 Chain of Responsibility
Found: Express middleware chain Assessment:- Properly used — express middleware implements this naturally.
- Error-handling middleware should remain last in the chain.
3.2 Strategy Pattern (Authentication)
Status: Missing (recommended) Assessment:- Current implementation supports only local email/password auth.
- If you plan to add OAuth, SSO, or LDAP, a strategy-based design keeps route code clean and extensible.
3.3 Observer Pattern (Eventing)
Found: DB connection events (limited application eventing) Assessment:- App-level events (e.g.,
user:login:success) are not centralized. - Introduce an event emitter to decouple side effects (audit logs, metrics, rate-limiting) from core logic.
3.4 Command Pattern
Status: Not implemented / Not required for current scope Assessment:- Not necessary for synchronous HTTP request/response flows.
- Consider when adding background job queues or task orchestration.
4. DOMAIN PATTERNS
4.1 Repository Pattern
Status: Missing — raw SQL in route handlers Issue:- Routes contain SQL and DB logic, violating separation of concerns.
- Tests and business logic become harder to maintain.
UserRepository and move DB queries out of routes.
4.2 Service Layer Pattern
Status: Missing — business logic lives in routes Issue:- Authentication and token generation are in HTTP handlers.
- Moving core logic to a service layer isolates business rules and simplifies testing.
AuthService dependent on UserRepository and TokenFactory.
4.3 DTO Pattern
Status: Partial / Informal Issue:- Responses build objects inline; there is no consistent public view.
- Introduce simple DTOs to standardize API responses and avoid leaking internal fields.
res.json({ token, user: new UserDTO(user) });
Importance: 5/10 — useful for API contract consistency.
4.4 Domain Model Pattern
Status: Anemic domain (missing domain entities) Issue:- No domain entities with encapsulated behavior; business logic is scattered.
- Introduce a minimal
Userdomain object that owns behavior like password verification and public projection.
Summary of Recommendations
Critical (9/10)- Extract a
UserRepositoryto remove raw SQL from routes. - Implement an
AuthServiceto host authentication logic and token orchestration.
- Add JWT authentication middleware and protect endpoints.
- Consider a Strategy pattern for multiple authentication methods.
- Introduce a centralized
TokenFactoryfor token creation and rotation.
- Add application-level events (Observer) for audit and rate-limiting hooks.
- Introduce DTOs for consistent API responses.
- Consider a small query-caching proxy (Proxy pattern) for read-heavy endpoints.
- Enrich domain models and add domain behaviors.
- Builder/Command patterns are not necessary now but useful later for complexity.
- Extract
UserRepositoryand update routes to use it. - Create
AuthServiceand move business logic from routes into it. - Add JWT authentication middleware and protect endpoints.
- Implement
TokenFactory. - Add app-level events and optional caching proxy.
Prioritize extracting repository and service layers first — these two changes immediately improve testability, separation of concerns, and make adding additional patterns (strategy, factory, eventing) straightforward.

Links and References
- Express.js documentation
- PostgreSQL node client (pg)
- jsonwebtoken (JWT for Node.js)
- express-validator