How the-pujon/express-starterx works
1. This repository is a TypeScript Node.js/Express REST API starter built around Clean Architecture. The entrypoint is split between src/server.ts and src/app.ts, with app.ts assembling Express middleware and server.ts starting the HTTP listener.
2. The application is organized into feature modules under src/app/modules/, primarily Auth, User, and ActivityLog. Each module is divided into presentation, application, domain, and infrastructure, so HTTP handling, business use cases, domain models, and technical persistence are kept separate.
3. The HTTP boundary lives in the presentation layer. Routes such as auth.route.ts and user.route.ts wire controllers, request validation, and middleware together. Controllers are thin and delegate to application services rather than talking directly to the database.
4. The application layer contains the use-case services that coordinate business flow. In Auth, this includes registration, authentication, token handling, email verification, and password reset services. In User, services cover profile and user management operations. These services depend on repository interfaces, not concrete storage implementations.
5. The domain layer defines the core contracts and entities. It contains model classes like user.model.ts, auth-session.model.ts, and activity-log.model.ts, plus repository interfaces and domain-specific exceptions/config. This is where business rules are meant to live without Express, Mongo, Redis, or email details leaking in.
6. The infrastructure layer contains the concrete integrations. MongoDB is used through Mongoose schemas and repositories for users, histories, user profiles, and activity logs. Redis is used for auth session storage and other short-lived state. A Google OAuth adapter is present as an external auth integration.
7. There is a clear composition-root pattern in the route files: repositories and services are instantiated there and injected into controllers. That means dependency wiring happens at the edge, while the inner layers stay framework-agnostic and testable.
8. Cross-cutting concerns are handled in shared middleware and helpers under src/app/middlewares/, src/app/errors/, and src/app/utils/. This includes authentication middleware, request validation, global error handling, not-found handling, response formatting, email sending, Redis utilities, and async error wrapping.
9. The repo includes an event-driven side channel via src/app/shared/events/. Domain and application flows can emit events, and activity-log.listener.ts consumes them to persist audit/activity records. This creates a secondary path from core business actions to logging without hard-coupling logging into every service.
10. Email is part of the auth architecture. HTML templates in templates/ support verification, reset-password, and welcome emails, while utility code handles delivery. This makes outbound email a separate external dependency from the main request/response flow.
11. Configuration is centralized in src/app/config/ and environment files. The runtime expects MongoDB, Redis, JWT secrets, and optional email/cloud services. docker-compose.yml and the Dockerfile show that local infrastructure is meant to be brought up as companion services rather than embedded in the app.
12. Testing is split into unit tests and E2E tests under src/__tests__/. There are module-level tests for Auth services and an end-to-end auth test suite, which fits the architecture’s emphasis on testing isolated services and route flows separately.
13. The repository also includes developer tooling around the architecture: createModule.js for generating new module structure, ecosystem.config.js for process management, and ARCHITECTURE.md/readme.md for documentation. These support the modular pattern rather than defining runtime behavior.
14. For an architecture diagram, the main boundaries to show are: client requests into Express, route/controller entry points, application services, domain contracts/models, infrastructure adapters/repositories, and external systems such as MongoDB, Redis, email delivery, and Google OAuth. The activity-log event listener is best shown as an asynchronous offshoot from the core auth/user flows.
Main entry points: registration.service.ts Auth Use Cases · mongo-user.repository.ts Auth Infra · app.ts Express App · index.ts Routes
Components 22
Bootstrap
Process entrypoint and app assembly
- Server · Starts the HTTP listener
src/server.ts - Express App · Assembles middleware and application wiring
src/app.ts - Routes · Registers module route trees
src/app/routes/index.ts
Auth
Authentication and account flows
- Auth API · Auth routes, controllers, and request validation
src/app/modules/Auth/presentation/routes/auth.route.ts - Auth Use Cases · Registration, login, token, verification, and reset flows
src/app/modules/Auth/application/services/registration.service.ts - Auth Domain · Auth entities, contracts, and rules
src/app/modules/Auth/domain/models/user.model.ts - Auth Infra · Mongo, Redis, and OAuth adapters for auth
src/app/modules/Auth/infrastructure/persistence/repositories/mongo-user.repository.ts
User
User profile and management flows
- User API · User routes and controllers
src/app/modules/User/presentation/routes/user.route.ts - User Use Cases · Profile and user management services
src/app/modules/User/application/services/user-profile.service.ts - User Domain · User profile entities and repository contracts
src/app/modules/User/domain/models/user-profile.model.ts - User Infra · Mongo-backed user profile repository
src/app/modules/User/infrastructure/repositories/mongo-user-profile.repository.ts
Activity Log
Audit/event-driven logging
- Log Service · Builds activity log records from events
src/app/modules/ActivityLog/application/services/activity-log.service.ts - Log Domain · Activity log entity and repository contract
src/app/modules/ActivityLog/domain/models/activity-log.model.ts - Log Repo · Mongo repository for audit logs
src/app/modules/ActivityLog/infrastructure/repositories/mongo-activity-log.repository.ts
Shared
Cross-cutting middleware, events, and utilities
- Event Bus · Shared domain/application event emitter
src/app/shared/events/event-emitter.ts - Log Listener · Consumes events and persists activity records
src/app/shared/events/activity-log.listener.ts - Middleware · Auth, validation, and error-handling middleware
src/app/middlewares/globalErrorhandler.ts - Utilities · Async wrapping, email, Redis, and response helpers
src/app/utils/sendEmail.ts
External
Datastores and third-party services
- MongoDB · Primary document database
- Redis · Short-lived auth/session storage
- Email Service · Outbound email delivery and templates
templates/verification-email.html - Google OAuth · Third-party auth integration
src/app/modules/Auth/infrastructure/adapters/google-oauth.adapter.ts
Connections 23
- Server → Express App: boots · Starts the Express application
- Express App → Routes: mounts · Registers the route tree and middleware stack
- Express App → Middleware: uses · Applies shared request and error middleware
- Routes → Auth API: auth · Mounts the auth HTTP boundary
- Routes → User API: user · Mounts the user HTTP boundary
- Auth API → Auth Use Cases: calls · Delegates HTTP requests to use cases
- Auth Use Cases → Auth Domain: depends on · Uses domain contracts and entities
- Auth Use Cases → Auth Infra: persists via · Relies on repository implementations and adapters
- Auth Infra → MongoDB: stores · Uses MongoDB for durable auth data
- Auth Infra → Redis: sessions · Stores auth sessions and short-lived state
- Auth Infra → Google OAuth: oauth · Bridges external Google authentication
- Auth Use Cases → Utilities: emails · Uses helpers for messaging and async flow
- User API → User Use Cases: calls · Delegates user requests to use cases
- User Use Cases → User Domain: depends on · Uses user profile entities and interfaces
- User Use Cases → User Infra: persists via · Relies on user profile repository implementations
- User Infra → MongoDB: stores · Persists user profile data in MongoDB
- Event Bus → Log Listener: publishes · Emits domain events to the audit listener
- Log Listener → Log Service: handles · Transforms events into log writes
- Log Service → Log Domain: depends on · Uses activity log contracts and model
- Log Service → Log Repo: persists via · Stores logs through the repository
- Log Repo → MongoDB: stores · Persists audit records in MongoDB
- Middleware → Event Bus: observes · Supports request flows that can emit events
- Utilities → Email Service: sends · Delivers verification and reset emails