This is a backend server for a Food App built using Node.js, Express, MongoDB, and JWT for authentication. It includes routes for user authentication, restaurant management, food categories, food items, and order placement.
- Node.js: Ensure you have Node.js installed on your machine. If not, download and install Node.js from here.
- MongoDB Atlas: You should have a MongoDB Atlas account and a cluster set up. The MongoDB URL will be used for connecting the backend to the database.
- Clone the Repository
First, clone the repository to your local machine:git clone https://github.com/Reeteshrajesh/nodejs-food-app.git cd nodejs-food-app
- Install Dependencies
After navigating to the project directory, run the following command to install all required dependencies:
npm install
- Create .env File
Create a .env file in the root directory of the project and add the following environment variables:
PORT=8080 MONGO_URL=mongodb+srv://<username>:<password>@cluster0.l98frwm.mongodb.net/food-app JWT_SECRET=mysecretvalue
Replace and with your MongoDB Atlas credentials.
- Run the Server
After setting up the .env file, run the following command to start the server:
npm run server
The server will now be running on the specified port (default is 8080).
POST /api/v1/auth/register
Registers a new user.
Request body:
{
"email": "user@example.com",
"password": "password123"
}
GET /api/v1/auth/login
Logs in an existing user.
Request body:
{
"email": "user@example.com",
"password": "password123"
}
GET /api/v1/user
Get details of the logged-in user (requires authentication).
Headers:
Authorization: Bearer <JWT_Token>
PUT /api/v1/user
Update the details of the logged-in user (requires authentication).<br>
***Request body:***
{
"name": "New Name",
"email": "newemail@example.com"
}
POST /api/v1/user/update-password
Update the user's password (requires authentication).
Request body:
{
"currentPassword": "oldpassword",
"newPassword": "newpassword123"
}
POST /api/v1/user/reset-password
Reset the user's password (requires email verification).
Request body:
{
"email": "user@example.com"
}
DELETE /api/v1/user
Delete the logged-in user's account (requires authentication).
Headers:
Authorization: Bearer <JWT_Token>
POST /api/v1/resturant
Create a new restaurant (requires admin authentication).<br>
***Request body:***
{
"name": "Restaurant Name",
"address": "Restaurant Address"
}
GET /api/v1/resturant
Get a list of all restaurants.
GET /api/v1/resturant/:id
Get details of a specific restaurant by ID.
DELETE /api/v1/resturant/:id
Delete a restaurant by ID (requires admin authentication).
GET /api/v1/category
Get a list of all food categories.
POST /api/v1/category
Create a new food category (requires admin authentication).<br>
***Request body:***
{
"name": "Category Name"
}
PUT /api/v1/category/:id
Update a food category by ID (requires admin authentication).
Request body:
{
"name": "Updated Category Name"
}
DELETE /api/v1/category/:id
Delete a food category by ID (requires admin authentication).
POST /api/v1/food
Create a new food item (requires admin authentication).<br>
***Request body:***
{
"name": "Food Item",
"price": 10.99,
"categoryId": "category_id",
"restaurantId": "restaurant_id"
}
GET /api/v1/food
Get a list of all food items.
GET /api/v1/food/:id
Get details of a specific food item by ID.
GET /api/v1/food/restaurant/:restaurantId
Get all food items by a specific restaurant.
PUT /api/v1/food/:id
Update a food item by ID (requires admin authentication).
Request body:
{
"name": "Updated Food Item",
"price": 12.99
}
DELETE /api/v1/food/:id
Delete a food item by ID (requires admin authentication).
GET /api/v1/order
Place a new order.<br>
***Request body:***
{
"foodItems": [
{
"foodId": "food_id",
"quantity": 2
}
],
"userId": "user_id",
"restaurantId": "restaurant_id"
}
GET /api/v1/order/status/:orderId
Get the status of a specific order by ID.
PORT: The port the server will run on (default: 8080).
MONGO_URL: The MongoDB connection URL for your MongoDB Atlas cluster.
JWT_SECRET: The secret used for generating and verifying JWT tokens.
Node.js: JavaScript runtime for the server-side code.
Express: Web framework for building the API.
MongoDB: NoSQL database for storing app data.
JWT: JSON Web Token for authentication and authorization.
bcryptjs: Library for hashing passwords securely.
dotenv: Loads environment variables from a .env file.
morgan: HTTP request logger middleware.
cors: Middleware to enable cross-origin requests.
This project is developed and maintained by [Reetesh kumar].
- GitHub: @Reetesh kumar
- LinkedIn: @Reetes hKumar
- Twitter: @uttam_reetesh
MIT
Pull requests are welcome.
For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.