How fabiobatoni/daily-diet-api works
Project type and purpose
• This is a TypeScript-based Node.js REST API for managing users and their meals (a diet tracker).
• It provides endpoints to register users, authenticate requests, perform CRUD on meals, and compute user metrics.
File structure
• Top level: configuration files (.eslintrc.json, tsconfig.json), package.json, README.
• src/
• env/index.ts – loads environment variables (e.g. DB connection, JWT secret)
• app.ts – sets up the application: middleware, routes, error handling
• server.ts – HTTP server bootstrap (binds app to a port)
• No frontend code—pure backend.
README for additional insights
• Functional requirements: user signup, authentication (identify user per request), meal CRUD with timestamps, diet flag, per-user authorization, and reporting metrics (total meals, in-diet count, out-diet count, best in-diet streak).
Main components
a. Client (Postman or UI)
b. HTTP Server (Express or Fastify instance in server.ts)
c. Application Layer (app.ts):
• Middleware:
• Logging
• Body parser
• Authentication (JWT/session) – verifies user identity on each request
• Routes:
• /users → UserController
• /sessions → AuthController
• /meals → MealController
• /metrics → MetricsController
• Controllers → call corresponding Services
d. Services (business logic):
• UserService
• AuthService
• MealService
• MetricsService
e. Data Access Layer (Repositories/ORM):
• UserRepository
• MealRepository
f. Database (relational, e.g. PostgreSQL or SQLite)
g. Configuration (env module)
h. Optional: Error handling & logging components
Relationships and interactions
• Client → HTTP Server (HTTP request)
• Server loads env → initializes app
• Request → Middleware (auth check) → Route matches → Controller
• Controller → Service → Repository → Database
• Database → Repository → Service → Controller → HTTP Response → Client
Architectural patterns and design principles
• Layered architecture (Routing → Controller → Service → Repository → DB)
• Separation of concerns (auth in middleware, config in env module)
• Single Responsibility (each service handles one domain: users, meals, metrics)
• Dependency injection (pass repositories into services)
Technologies and frameworks
• Node.js + TypeScript
• Express or Fastify (inferred)
• JWT or session middleware for auth
• ORM/Query builder (e.g., Prisma, TypeORM, or Knex)
• dotenv or similar for env variables
Essential elements
• Entrypoint (server.ts) and app initialization (app.ts)
• Middleware chain (parsers, auth)
• Controllers for each route group
• Service layer with business logic
• Repository/ORM layer
• Environment/configuration module