This is the official backend API for NutriTrack, a mobile and web app designed to help Nigerians track their nutrition, log local foods, and receive smart suggestions based on their fitness goals.
This server is built with Node.js, Express, and MongoDB.
- JWT Authentication: Secure user registration (
/register) and login (/login) using JSON Web Tokens. - Protected Routes: Middleware protects all sensitive app endpoints, ensuring only logged-in users can access their data.
- Meal Logging (CRUD): A full "Create, Read, Delete" system for logging meals (
/api/meals/log,/api/meals/me,/api/meals/:id). - Custom Food Database: A pre-seeded database (
foodscollection) containing 25+ common Nigerian and general foods with full nutritional data. - Smart Suggestions: An endpoint (
/api/suggestions) that reads a user'sgoal(e.g., "weight_loss") and returns a filtered list of food suggestions. - Profile Management: A route (
/api/users/me) to update a user's profile information, like their name or fitness goal. - Community & AI:
GET /api/tips/random: Provides static, pre-loaded tips from the database.POST /api/ai/get-advice: Integrates with the Google Gemini API to provide smart, AI-powered advice.
- Runtime: Node.js
- Framework: Express.js
- Database: MongoDB (using Mongoose)
- Authentication: JSON Web Tokens (JWT)
- AI: Google Generative AI (Gemini)
- Utilities:
bcryptjs(password hashing),dotenv(environment variables),cors(cross-origin requests),nodemon(live-reload development).
To get a local copy up and running, follow these simple steps.
- Node.js (which includes
npm) - A MongoDB Atlas account (for your database)
- A Google AI Studio API Key (for the Gemini AI)
-
Clone the repository:
git clone [https://github.com/your-username/nutritrack-api.git](https://github.com/your-username/nutritrack-api.git) cd nutritrack-api -
Install NPM packages:
npm install
-
Create your Environment File: Create a file named
.envin the root of the project. This file holds all your secret keys. -
Populate your
.envfile: Copy the following and paste it into your.envfile, replacing the values with your own keys.# Your MongoDB connection string from Atlas DB_CONNECTION_STRING=mongodb+srv://your-user:[email protected]/ # A long, random string used to sign your JWTs JWT_SECRET=thisIsAReallyStrongSecretKey123! # Your API key from Google AI Studio GEMINI_API_KEY=your-gemini-api-key-goes-here
This command uses nodemon to start your server, which will automatically restart every time you save a file.
npm run dev