# ethic0/versejam-nodejs-backend architecture

> Architecture diagram of the GitHub repository ethic0/versejam-nodejs-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ethic0/versejam-nodejs-backend
- Repository: https://github.com/ethic0/versejam-nodejs-backend

## Overview

#### Purpose and runtime

VerseJAM is a Node.js REST API implementing the VerseJAM frontend contract for poem publishing, comments, profiles, tags, and user authentication. It runs as an Express server backed by MongoDB through Mongoose. Core startup is in `app.js`, with environment/application settings centralized in `config/index.js`.

#### Application bootstrap and HTTP boundary

`app.js` creates the Express application, connects Mongoose to MongoDB, loads models/auth configuration, and mounts routing. The API boundary is split between general site routing in `routes/index.js`, authentication middleware in `routes/auth.js`, and version/domain API routing under `routes/api/`.

#### API composition and error policy

`routes/api/index.js` is the API router aggregator and owns API-level error handling, including translation of Mongoose validation failures into client-readable 422 responses. It delegates resource behavior to:
- `routes/api/poems.js`
- `routes/api/users.js`
- `routes/api/profiles.js`
- `routes/api/tags.js`

This keeps HTTP resource operations separate from persistence schema definitions.

#### Authentication and identity flow

JWT authentication is enforced through reusable Express middleware in `routes/auth.js`. `required` rejects unauthenticated calls; `optional` permits anonymous access while exposing verified JWT claims as `req.payload`. Passport setup in `config/passport.js` supplies credential/authentication strategy integration, while users are persisted through `models/User.js`. Tokens are generated/validated using `jsonwebtoken` and `express-jwt`.

#### Core content domain

Poems are the central publishable resource, modeled in `models/Poem.js` and exposed through `routes/api/poems.js`. Poem routes handle CRUD-style operations and likely apply authenticated identity from `req.payload` for author-owned actions. URL-friendly poem identifiers are supported by the `slug` dependency, creating a stable route-facing identifier separate from MongoDB object IDs.

Comments are a related interaction resource defined by `models/Comment.js`; poem-facing operations are expected to create, retrieve, or remove comments through the poem API surface. User-facing author/profile data spans `models/User.js` and `routes/api/profiles.js`.

#### Persistence relationships and external services

MongoDB is the sole material external service and must be running before the API starts. Mongoose schemas in `models/User.js`, `models/Poem.js`, and `models/Comment.js` define the persistence boundary and document relationships. `mongoose-unique-validator` normalizes uniqueness-index failures into Mongoose validation errors, which then cross the API error boundary in `routes/api/index.js`.

#### Main control flow

A request enters Express via `app.js`, is dispatched through `routes/index.js` and `routes/api/index.js`, optionally passes JWT middleware from `routes/auth.js`, then reaches a domain router such as `routes/api/poems.js`. The handler reads or mutates Mongoose documents, serializes the API response, and routes validation failures to centralized error middleware. Authentication configuration flows from `config/index.js` into `config/passport.js` and `routes/auth.js`.

Main entry points: [`routes/api/index.js`](https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/api/index.js) (API router & errors), [`routes/api/poems.js`](https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/api/poems.js) (Poem endpoints), [`models/User.js`](https://github.com/ethic0/versejam-nodejs-backend/blob/master/models/User.js) (User schema), [`app.js`](https://github.com/ethic0/versejam-nodejs-backend/blob/master/app.js) (Express application)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_bootstrap["Bootstrap &amp; config"]
  node_app["Express application<br/>Node.js bootstrap<br/>[app.js]"]
  node_settings["Application settings<br/>configuration<br/>[index.js]"]
  node_passport["Passport setup<br/>authentication strategy<br/>[passport.js]"]
end

subgraph group_http["HTTP API"]
  node_site_routes["Site router<br/>Express router<br/>[index.js]"]
  node_api_router["API router &amp; errors<br/>Express router<br/>[index.js]"]
  node_auth["JWT middleware<br/>Express middleware<br/>[auth.js]"]
end

subgraph group_domain["Content domain"]
  node_poem_routes["Poem endpoints<br/>resource router<br/>[poems.js]"]
  node_user_routes["User endpoints<br/>resource router<br/>[users.js]"]
  node_profile_routes["Profile endpoints<br/>resource router<br/>[profiles.js]"]
  node_tag_routes["Tag endpoints<br/>resource router<br/>[tags.js]"]
end

subgraph group_persistence["Persistence"]
  node_user_model["User schema<br/>Mongoose model<br/>[User.js]"]
  node_poem_model["Poem schema<br/>Mongoose model<br/>[Poem.js]"]
  node_comment_model["Comment schema<br/>Mongoose model<br/>[Comment.js]"]
  node_mongoose["Mongoose<br/>ODM"]
  node_mongodb[("MongoDB<br/>external database")]
end

node_settings -->|"configures"| node_app
node_settings -->|"configures auth"| node_passport
node_app -->|"connects"| node_mongoose
node_mongoose -->|"persists to"| node_mongodb
node_app -->|"mounts"| node_site_routes
node_site_routes -->|"dispatches API requests"| node_api_router
node_api_router -->|"uses identity middleware"| node_auth
node_passport -->|"supplies strategy"| node_auth
node_api_router -->|"delegates"| node_poem_routes
node_api_router -->|"delegates"| node_user_routes
node_api_router -->|"delegates"| node_profile_routes
node_api_router -->|"delegates"| node_tag_routes
node_poem_routes -->|"reads and mutates"| node_poem_model
node_poem_routes -->|"manages comments"| node_comment_model
node_poem_routes -->|"resolves author"| node_user_model
node_user_routes -->|"reads and mutates"| node_user_model
node_profile_routes -->|"reads profiles"| node_user_model
node_user_model -->|"registered schema"| node_mongoose
node_poem_model -->|"registered schema"| node_mongoose
node_comment_model -->|"registered schema"| node_mongoose
node_mongoose -.->|"validation errors"| node_api_router

click node_app "https://github.com/ethic0/versejam-nodejs-backend/blob/master/app.js"
click node_settings "https://github.com/ethic0/versejam-nodejs-backend/blob/master/config/index.js"
click node_passport "https://github.com/ethic0/versejam-nodejs-backend/blob/master/config/passport.js"
click node_site_routes "https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/index.js"
click node_api_router "https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/api/index.js"
click node_auth "https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/auth.js"
click node_poem_routes "https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/api/poems.js"
click node_user_routes "https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/api/users.js"
click node_profile_routes "https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/api/profiles.js"
click node_tag_routes "https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/api/tags.js"
click node_user_model "https://github.com/ethic0/versejam-nodejs-backend/blob/master/models/User.js"
click node_poem_model "https://github.com/ethic0/versejam-nodejs-backend/blob/master/models/Poem.js"
click node_comment_model "https://github.com/ethic0/versejam-nodejs-backend/blob/master/models/Comment.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_settings,node_passport toneBlue
class node_site_routes,node_api_router,node_auth toneAmber
class node_poem_routes,node_user_routes,node_profile_routes,node_tag_routes toneMint
class node_user_model,node_poem_model,node_comment_model,node_mongoose,node_mongodb toneRose
```

## Components

### Bootstrap & config

- [Express application](https://github.com/ethic0/versejam-nodejs-backend/blob/master/app.js): Node.js bootstrap · Creates Express, connects Mongoose, and mounts routes. · `app.js`
- [Application settings](https://github.com/ethic0/versejam-nodejs-backend/blob/master/config/index.js): configuration · `config/index.js`
- [Passport setup](https://github.com/ethic0/versejam-nodejs-backend/blob/master/config/passport.js): authentication strategy · `config/passport.js`

### HTTP API

- [Site router](https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/index.js): Express router · `routes/index.js`
- [API router & errors](https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/api/index.js): Express router · Aggregates domain routes and maps validation failures to 422 responses. · `routes/api/index.js`
- [JWT middleware](https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/auth.js): Express middleware · Provides required and optional JWT identity handling. · `routes/auth.js`

### Content domain

- [Poem endpoints](https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/api/poems.js): resource router · Handles poem CRUD and comment-facing operations. · `routes/api/poems.js`
- [User endpoints](https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/api/users.js): resource router · `routes/api/users.js`
- [Profile endpoints](https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/api/profiles.js): resource router · `routes/api/profiles.js`
- [Tag endpoints](https://github.com/ethic0/versejam-nodejs-backend/blob/master/routes/api/tags.js): resource router · `routes/api/tags.js`

### Persistence

- [User schema](https://github.com/ethic0/versejam-nodejs-backend/blob/master/models/User.js): Mongoose model · `models/User.js`
- [Poem schema](https://github.com/ethic0/versejam-nodejs-backend/blob/master/models/Poem.js): Mongoose model · Stores publishable poems and route-facing slugs. · `models/Poem.js`
- [Comment schema](https://github.com/ethic0/versejam-nodejs-backend/blob/master/models/Comment.js): Mongoose model · Related interaction documents for poems. · `models/Comment.js`
- **Mongoose**: ODM · Schema validation and document persistence boundary.
- **MongoDB**: external database · Required backing datastore.

## Connections

- Application settings → Express application: configures
- Application settings → Passport setup: configures auth
- Express application → Mongoose: connects
- Mongoose → MongoDB: persists to
- Express application → Site router: mounts
- Site router → API router & errors: dispatches API requests
- API router & errors → JWT middleware: uses identity middleware
- Passport setup → JWT middleware: supplies strategy
- API router & errors → Poem endpoints: delegates
- API router & errors → User endpoints: delegates
- API router & errors → Profile endpoints: delegates
- API router & errors → Tag endpoints: delegates
- Poem endpoints → Poem schema: reads and mutates
- Poem endpoints → Comment schema: manages comments
- Poem endpoints → User schema: resolves author
- User endpoints → User schema: reads and mutates
- Profile endpoints → User schema: reads profiles
- User schema → Mongoose: registered schema
- Poem schema → Mongoose: registered schema
- Comment schema → Mongoose: registered schema
- Mongoose → API router & errors: validation errors · Converted to client-readable 422 responses.
