How diogocunhacardoso/coinllector-api-express-ts works
Project Type and Purpose
• This is a monolithic, TypeScript-based REST API backend (“full-stack” on the server side) for an application that lets users collect and manage European coin faces.
• It exposes HTTP endpoints (Express), persists data in MongoDB (via Mongoose), handles authentication/authorization (JWT, sessions, cookies, RBAC), processes images via Cloudinary, and uses Redis for rate limiting or session caching.
High-Level File Structure & Architectural Patterns
• Entry point: src/app.ts / src/routes.ts
• Layered architecture:
• Middleware (authentication, error handling, validation, rate limiting)
• Routing (src/routes/*.route.ts)
• Controllers (src/controller/*.controller.ts)
• Services / business logic (src/service/*.service.ts)
• Models / persistence (src/models/*.model.ts using Mongoose)
• Schema/validation (Zod schemas in src/schema)
• Utils & constants (logging, JWT, email, cloudinary client, etc.)
• Supplemental components: Cron jobs (in utils or separate scheduler module), Swagger docs, Prometheus metrics endpoint, tests.
Key Components
a. External Clients and Entry Points
• User’s browser or mobile app (REST/HTTPS)
• Swagger UI (docs endpoint)
b. API Layer (running in Docker container / Node.js runtime)
• Load Balancer or API Gateway (if present; optional)
• Express Server
• Middleware Pipeline (CORS, Helmet, JSON parser, rate limiter, deserializeUser)
• Route Handlers → Controllers
• Controllers invoke Services
• Services invoke Models / Utilities
c. Persistence & Cache
• MongoDB cluster (collections: users, sessions, coins, countries, verification codes)
• Redis instance (rate limiter, session cache)
d. External Services
• Cloudinary (image upload, manipulation)
• Email SMTP service (via nodemailer)
• Google OAuth provider (for social login flow)
• Prometheus (scrapes metrics endpoint)
e. Auxiliary Components
• Cron scheduler (cleanup jobs for inactive/unverified accounts)
• Logger (Pino) writing to stdout / files
• Swagger docs served from /swagger
Architectural Patterns & Design Principles
• Layered separation: each layer has its own responsibility.
• Single-responsibility services (one service per entity: coin, country, session, user)
• Middleware chain for cross-cutting concerns (error handling, validation, auth).
• Use of cron/scheduled tasks to handle background jobs.
• Externalize configuration via environment variables (.env).
• Observability via Prometheus metrics and structured logs.