How ethic0/versejam-nodejs-backend works
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: index.js API router & errors · poems.js Poem endpoints · User.js User schema · app.js Express application
Components 15
Bootstrap & config
- Express application · Creates Express, connects Mongoose, and mounts routes.
app.js - Application settings
config/index.js - Passport setup
config/passport.js
HTTP API
- Site router
routes/index.js - API router & errors · Aggregates domain routes and maps validation failures to 422 responses.
routes/api/index.js - JWT middleware · Provides required and optional JWT identity handling.
routes/auth.js
Content domain
- Poem endpoints · Handles poem CRUD and comment-facing operations.
routes/api/poems.js - User endpoints
routes/api/users.js - Profile endpoints
routes/api/profiles.js - Tag endpoints
routes/api/tags.js
Persistence
- User schema
models/User.js - Poem schema · Stores publishable poems and route-facing slugs.
models/Poem.js - Comment schema · Related interaction documents for poems.
models/Comment.js - Mongoose · Schema validation and document persistence boundary.
- MongoDB · Required backing datastore.
Connections 21
- 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.