How karthikeyan7333/nutrition-tracker works
Purpose and Entry Points
Nutrition Tracker is a MERN application for authenticated nutrition, hydration, goals, and progress tracking, preserving an earlier AI-assisted food-entry experience. Development starts both services through the root package scripts; runtime entry points are backend/server.js for Express and frontend/src/main.tsx for the Vite React client.
Frontend Application Shell
The React SPA mounts the application and owns browser-side navigation, authenticated UI state, and dashboard composition. frontend/src/App.tsx routes users between login/register and the dashboard; frontend/src/pages/DashboardContainer.tsx coordinates the primary tracker screen. Vercel serves the built SPA and rewrites browser-route refreshes via frontend/vercel.json.
Client API Boundary
The frontend uses a centralized Axios client in frontend/src/lib/api.ts to call backend /api endpoints. Locally, Vite proxies these calls to Express; in production, VITE_API_BASE_URL targets the Render backend. UI components such as frontend/src/components/AddFoodCard.tsx, frontend/src/components/WaterTracker.tsx, and frontend/src/components/GoalEditor.tsx submit and refresh domain data through this boundary, never accessing database or Gemini credentials.
Express API and Request Pipeline
backend/server.js initializes Express, database connectivity, middleware, and route mounting. Route modules partition public authentication from protected tracker resources: backend/routes/authRoutes.js, backend/routes/foodLogRoutes.js, and backend/routes/progressRoutes.js. JWT validation in backend/middleware/auth.js establishes the user identity before protected controllers execute; backend/middleware/errorHandler.js centralizes failed-request handling.
Domain Controllers and Persistence
Controllers implement API behavior for account access and user-scoped tracking records: backend/controllers/authController.js, backend/controllers/foodLogController.js, backend/controllers/goalController.js, backend/controllers/waterLogController.js, and backend/controllers/progressController.js. Mongoose models define MongoDB Atlas persistence for identities and tracker entities in backend/models/User.js, backend/models/FoodLog.js, backend/models/Goal.js, and backend/models/WaterLog.js. Database configuration is isolated in backend/config/database.js.
AI Food Analysis
Food creation can cross an internal service boundary to Gemini through backend/services/geminiService.js. The backend retains the Gemini key, prompt/schema behavior, and nutrition fallback behavior; controllers consume the service rather than exposing Gemini directly to the client. This keeps AI output subject to server-side validation, user association, and persistent food-log workflows.
Infrastructure, Deployment, and Migration
MongoDB Atlas is the system of record; Render hosts the Express service using render.yaml, and Vercel hosts the frontend. Environment handling is split between backend/config/env.js, backend secrets, and build-time frontend API configuration. Legacy JSON data in data/db.json is not runtime storage; backend/scripts/migrateJsonData.js performs an optional idempotent import into Atlas.
Main entry points: server.js Express server · foodLogController.js Domain controllers · foodLogRoutes.js Tracker routes · migrateJsonData.js Legacy JSON migration
Components 19
React Client
- Vite entry
frontend/src/main.tsx - App routes
frontend/src/App.tsx - Dashboard
frontend/src/pages/DashboardContainer.tsx - Tracker controls · Food, water, and goal controls submit through the API client.
frontend/src/components/AddFoodCard.tsx - Axios API client · Uses Vite proxy locally and configured Render URL in production.
frontend/src/lib/api.ts - Vercel SPA hosting · Serves the client build and rewrites browser routes.
frontend/vercel.json
Express API
- Express server · Initializes middleware, database connection, and route mounting.
backend/server.js - Auth routes
backend/routes/authRoutes.js - Tracker routes · Food logs and progress route modules expose user-scoped tracker APIs.
backend/routes/foodLogRoutes.js - JWT middleware
backend/middleware/auth.js - Error handler
backend/middleware/errorHandler.js - Domain controllers · Handles account access plus food, goal, water, and progress operations.
backend/controllers/foodLogController.js - Gemini analysis · Keeps AI prompts, credentials, and nutrition fallback on the server.
backend/services/geminiService.js
Data & Deployment
- Database connection
backend/config/database.js - Tracker models · User, food log, goal, and water log schemas.
backend/models/User.js - MongoDB Atlas
- Render service · Hosts the Express API.
render.yaml - Legacy JSON migration · Idempotently imports legacy records into Atlas.
backend/scripts/migrateJsonData.js - Legacy JSON data · Not used as runtime storage.
data/db.json
Connections 20
- Vite entry → App routes: mounts
- App routes → Dashboard: routes authenticated users
- Dashboard → Tracker controls: composes
- Tracker controls → Axios API client: submits and refreshes
- Axios API client → Express server: /api requests
- Vercel SPA hosting → Vite entry: serves build
- Render service → Express server: hosts
- Express server → Auth routes: mounts
- Express server → Tracker routes: mounts
- Express server → Error handler: uses
- Tracker routes → JWT middleware: validates identity
- Auth routes → Domain controllers: dispatches
- Tracker routes → Domain controllers: dispatches after auth
- Domain controllers → Tracker models: reads and writes
- Domain controllers → Gemini analysis: analyzes food entries
- Express server → Database connection: connects through
- Database connection → MongoDB Atlas: connects
- Tracker models → MongoDB Atlas: persists
- Legacy JSON migration → Legacy JSON data: imports from
- Legacy JSON migration → MongoDB Atlas: writes imported records