How idieguez/geopark-api works
Purpose and runtime
Geopark is a Node.js 22+/Express 5 REST backend for users to register, locate, and manage parked vehicles. It exposes JSON HTTP endpoints, persists operational data in MongoDB via Mongoose, and is intended to serve a separate client application. Production startup flows through src/index.js; the reusable Express application is assembled in src/app.js. Deployment support is indicated by Procfile.
Application composition and request boundary
src/app.js configures the Express request pipeline and mounts the three API domains: authentication, user account management, and vehicle management. It is the cross-cutting boundary for HTTP parsing, security middleware (Helmet, CORS, rate limiting), request logging, route dispatch, and final error handling.
• src/app.js
• src/routes/auth-routes.js
• src/middlewares/error-middleware.js
Authentication stage
Unauthenticated clients register or log in through the auth route/controller pair. Password handling uses bcrypt and successful login returns a stateless JWT. Protected account and vehicle operations pass through JWT verification middleware, which establishes the authenticated-user context used for authorization and data ownership.
• src/routes/auth-routes.js
• src/controllers/auth-controller.js
• src/middlewares/auth-middleware.js
User-account domain
The users domain exposes only operations on the authenticated caller’s account: read profile, patch permitted fields, and delete account. The controller mediates between authenticated request context, validated payloads, and the User persistence model.
• src/routes/users-routes.js
• src/controllers/users-controller.js
• src/models/User.js
Vehicle domain and ownership boundary
Vehicles are created and managed under the authenticated user, with license plate as the route-level resource identifier for detail, update, and deletion operations. Controllers must enforce that list and individual-resource operations remain scoped to the caller, preventing cross-user vehicle access.
• src/routes/vehicles-routes.js
• src/controllers/vehicles-controller.js
• src/models/Vehicle.js
Validation and error contract
Routes apply domain-specific Zod schemas through a common validation middleware before controllers execute. Failures and asynchronous exceptions converge on centralized error handling, using application-specific errors and a JSend-style response contract; development may expose diagnostic detail while production returns controlled messages.
• src/middlewares/validation-middleware.js
• src/schemas/vehicles-schema.js
• src/utils/app-error.js
Persistence, logging, and operations
src/database.js owns MongoDB connection lifecycle; Mongoose models define persisted users, vehicles, and logs. Environment-driven configuration is represented by .env.example. Tests run against an isolated in-memory MongoDB rather than the operational database, while the seed script supports initial Mongo data preparation.
• src/database.js
• src/models/Log.js
• scripts/mongo/seed.js
Main entry points: app.js Express application · database.js MongoDB lifecycle · users-routes.js User routes · vehicles-routes.js Vehicle routes
Components 18
Runtime
- Production startup
src/index.js
HTTP API
- Express application · Configures parsing, security, logging, dispatch, and final errors.
src/app.js - JWT authentication · Establishes authenticated-user context.
src/middlewares/auth-middleware.js - Request validation · Applies domain Zod schemas before controllers.
src/middlewares/validation-middleware.js - Central error handler · Returns controlled JSend-style error responses.
src/middlewares/error-middleware.js - Application errors
src/utils/app-error.js
Domains
- Auth routes
src/routes/auth-routes.js - Auth controller · Registers users, verifies passwords, and issues JWTs.
src/controllers/auth-controller.js - User routes
src/routes/users-routes.js - Users controller
src/controllers/users-controller.js - Vehicle routes · Uses license plate as the detail resource identifier.
src/routes/vehicles-routes.js - Vehicles controller · Scopes lists and individual vehicles to the authenticated owner.
src/controllers/vehicles-controller.js - Vehicle schemas
src/schemas/vehicles-schema.js
Data & Operations
- User model
src/models/User.js - Vehicle model
src/models/Vehicle.js - MongoDB lifecycle · Owns MongoDB connection lifecycle.
src/database.js - Log model
src/models/Log.js - Mongo seed script · Prepares initial Mongo data.
scripts/mongo/seed.js
Connections 23
- Production startup → MongoDB lifecycle: connects
- Production startup → Express application: starts
- Express application → Auth routes: mounts
- Express application → User routes: mounts
- Express application → Vehicle routes: mounts
- Express application → Central error handler: final handler
- Auth routes → Request validation: validates
- Auth routes → Auth controller: dispatches
- Auth controller → User model: creates and verifies users
- User routes → JWT authentication: requires JWT
- User routes → Request validation: validates
- User routes → Users controller: dispatches
- Users controller → User model: reads and updates
- Vehicle routes → JWT authentication: requires JWT
- Vehicle routes → Request validation: validates
- Vehicle routes → Vehicles controller: dispatches
- Request validation → Vehicle schemas: uses
- Vehicles controller → Vehicle model: owner-scoped CRUD
- User model → MongoDB lifecycle: persists through
- Vehicle model → MongoDB lifecycle: persists through
- Log model → MongoDB lifecycle: persists through
- Mongo seed script → MongoDB lifecycle: prepares
- Application errors → Central error handler: handled by