A Node.js API with Express.js and MongoDB that provides a food database with nutritional information and allows users to create and manage their own food items and meals.
This API project, built with Node.js, Express.js, and MongoDB, is designed to offer a detailed food database with nutritional information and to enable users to create and manage their own food items and meals. It provides a comprehensive resource for accessing macro information, supporting personalized dietary tracking and meal planning. By allowing users to input custom foods and recipes, the API facilitates informed nutritional choices and supports both individual and professional needs in managing diet and health.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
To run this project, you need to install the following software:
-
Node.js:
-
Node.js is required to run the server-side code of the application.
-
Installation:
- Visit the Node.js official website.
- Download and install the recommended version for your operating system.
- Verify the installation by running the following commands in your terminal:
node -v npm -v
-
-
MongoDB:
-
MongoDB is needed to manage the application's database.
-
Installation:
- Visit the MongoDB official website to download the Community Server.
- Follow the instructions for your operating system to complete the installation.
- Start the MongoDB server by running:
mongod
-
Follow these steps to set up a development environment:
-
Clone the Repository:
-
Clone the project repository to your local machine.
git clone https://github.com/tskxz/macros4ever
-
-
Navigate to Project Directory:
-
Change to the project directory.
cd macros4ever
-
-
Install Project Dependencies:
-
Install the necessary Node.js packages listed in
package.json
.npm install
-
-
Start the MongoDB Server:
-
Ensure MongoDB is running. Start the server if it's not already running.
mongod
-
-
Execute Seeder:
-
To populate your database with initial data, you need to run the seeder script.
-
This step is essential for setting up the initial data required for the application to function correctly.
-
Usage:
- Ensure your MongoDB server is running.
- Execute the following command to import the seed data into your database:
npm run data:import
-
This command will load predefined data into your database collections. Verify that the data has been correctly inserted by checking the database.
-
-
Run the Application:
-
Start the application server.
npm run dev
-
-
Verify Setup:
- Check if the application is running by visiting
http://localhost:3000
in your web browser.
- Check if the application is running by visiting
Repeat these steps as needed to configure or troubleshoot your development environment.
This section describes the available API endpoints and how to use them.
-
GET /api/foods
- Public: Retrieve a list of all food items.
- Usage: This endpoint is accessible by anyone and returns a list of all available food items.
-
GET /api/foods/myfoods
- Private: Retrieve all food items created by the authenticated user.
- Usage: Requires authentication. Returns food items that the currently logged-in user has created.
-
POST /api/foods/myfoods
- Private: Create a new food item for the authenticated user.
- Usage: Requires authentication. Allows users to add new food items to their collection.
-
POST /api/foods/publish/:id
- Private Admin: Publish a food item with the given ID.
- Usage: Requires authentication and admin privileges. Publishes a food item to make it available to all users.
-
GET /api/foods/:id
- Public: Retrieve a single food item with the given ID.
- Usage: This endpoint is accessible by anyone and returns details of a specific food item.
-
POST /api/foods/
- Private Admin: Create a new food item.
- Usage: Requires authentication and admin privileges. Allows administrators to add new food items to the database.
-
PUT /api/foods/:id
- Private Admin: Update a food item with the given ID.
- Usage: Requires authentication and admin privileges. Updates details of a specific food item.
-
DELETE /api/foods/:id
- Private Admin: Delete a food item with the given ID.
- Usage: Requires authentication and admin privileges. Removes a specific food item from the database.
-
POST /api/users/login
- Public: Log in a user.
- Usage: Allows users to log in by providing their credentials. Returns an authentication token.
-
POST /api/users/register
- Public: Register a new user.
- Usage: Allows new users to create an account by providing their details.
-
GET /api/users
- Private Admin: Retrieve a list of all users.
- Usage: Requires authentication and admin privileges. Returns a list of all registered users.
-
POST /api/users/logout
- Public: Log out a user.
- Usage: Logs out the currently logged-in user and invalidates their authentication token.
-
GET /api/users/profile
- Private: Retrieve the authenticated user's information.
- Usage: Requires authentication. Returns the profile details of the currently logged-in user.
-
GET /api/meals
- Private: Retrieve all meals created by the authenticated user.
- Usage: Requires authentication. Returns all meals that the currently logged-in user has created.
-
GET /api/meals/:id
- Private: Retrieve a single meal created by the authenticated user.
- Usage: Requires authentication. Returns details of a specific meal created by the currently logged-in user.
-
POST /api/meals
- Private: Create a new meal for the authenticated user.
- Usage: Requires authentication. Allows users to create a new meal by specifying food items and quantities.
-
DELETE /api/meals/:id
- Private: Delete a meal created by the authenticated user.
- Usage: Requires authentication. Deletes a specific meal created by the currently logged-in user.
- @tskxz - Idea & Initial work
See also the list of contributors who participated in this project.