Skip to content

Core Architecture

Relevant source files - app.js - database/mongodb.js - middlewares/error.middleware.js - models/subscription.model.js - models/user.model.js

The Subscription Tracker API is built on a layered Express.js architecture designed for security, scalability, and maintainability. The system employs a standard request-response pipeline augmented by specialized middleware for security (Arcjet) and a robust data modeling layer using Mongoose.

Request Pipeline and Middleware

The application processes incoming requests through a sequential stack of global and route-specific middlewares. The entry point app.js configures the global stack, which handles cross-cutting concerns like security, parsing, and static file serving before delegating to specific domain routers.

Request Flow Diagram

This diagram illustrates how a request transitions from the network into the application's internal logic.

[Flowchart Diagram]

Sources:

  • app.js: 1-42
  • middlewares/error.middleware.js: 1-37

For a detailed breakdown of each middleware's role and the execution order, see Request Pipeline and Middleware.

Routing and Controller Pattern

The application follows a modular routing pattern where app.js mounts domain-specific routers under the /api/v1 prefix. This separates the concerns of URI definition from the business logic executed in controllers.

Route Prefix Purpose Router File
/api/v1/auth Authentication & Session Management routes/auth.routes.js
/api/v1/users User Profile Management routes/user.routes.js
/api/v1/subscriptions Subscription Lifecycle routes/subscription.routes.js
/api/v1/workflows Automated Reminders (Upstash) routes/workflow.routes.js
/api/v1/test Diagnostic Tools routes/test.routes.js

Sources:

Data Models

The system state is managed via Mongoose models that enforce schema validation and encapsulate lifecycle logic. There are two primary entities: User and Subscription.

Entity Relationship Diagram

The following diagram maps the code entities to their logical relationships.

[Class Diagram]

Sources:

  • models/user.model.js: 3-28
  • models/subscription.model.js: 3-86

The Subscription model includes a critical pre('save') hook that automatically calculates the renewalDate based on the frequency (daily, weekly, monthly, or yearly) and updates the status to 'expired' if the date has passed. For details on these validation rules and hooks, see Data Models.

Database Connection

The application uses MongoDB as its primary data store. The connection is established during the server bootstrap process in app.js.

[Flowchart Diagram]

Sources:

The connection logic ensures that the application does not start if the DB_URI is missing or if the database is unreachable. For more information on environment-specific configurations and error handling, see Database Connection.