A comprehensive Node.js backend API for a restaurant food delivery application built with Express.js, MongoDB, and JWT authentication.
- User Management: Registration, login, profile management with role-based access (client, admin, vendor, driver)
- Restaurant Management: CRUD operations for restaurants with location coordinates
- Food Management: Complete food item management with categories and ratings
- Category Management: Food category organization
- Order Management: Place orders and track order status
- Authentication & Authorization: JWT-based authentication with middleware protection
- Password Management: Update and reset password functionality
- Backend: Node.js, Express.js
- Database: MongoDB with Mongoose ODM
- Authentication: JSON Web Tokens (JWT)
- Password Hashing: bcryptjs
- Environment Variables: dotenv
- CORS: Cross-Origin Resource Sharing enabled
- Development: Nodemon for auto-restart
Restaurant-Backend-API/
βββ config/
β βββ db.js # Database connection configuration
βββ controllers/
β βββ authController.js # Authentication logic
β βββ categoryController.js # Category management
β βββ foodController.js # Food item management
β βββ restaurantController.js # Restaurant management
β βββ userController.js # User profile management
βββ middleware/
β βββ authMiddleware.js # JWT authentication middleware
βββ models/
β βββ categoryModel.js # Category schema
β βββ foodModel.js # Food item schema
β βββ orderModel.js # Order schema
β βββ restaurantModel.js # Restaurant schema
β βββ userModel.js # User schema
βββ routes/
β βββ authRoutes.js # Authentication routes
β βββ categoryRoutes.js # Category routes
β βββ foodRoutes.js # Food routes
β βββ restaurantRoutes.js # Restaurant routes
β βββ testRoutes.js # Test routes
β βββ userRoutes.js # User routes
βββ .env # Environment variables
βββ .gitignore # Git ignore file
βββ package.json # Dependencies and scripts
βββ server.js # Main server file
- Node.js (v14 or higher)
- MongoDB database
- npm or yarn package manager
-
Clone the repository
git clone <repository-url> cd Restaurant-Backend-API
-
Install dependencies
npm install
-
Environment Configuration
Create a
.envfile in the root directory and configure the following variables:PORT=8080 MONGO_URL=your_mongodb_connection_string JWT_SECRET=your_jwt_secret_key
-
Start the server
# Development mode with auto-restart npm start # Or directly with node node server.js
The server will start on http://localhost:8080
http://localhost:8080/api/v1
Most endpoints require JWT authentication. Include the token in the Authorization header:
Authorization: Bearer <your_jwt_token>
POST /auth/register- Register a new userPOST /auth/login- User login
GET /user/getUser- Get current user profilePUT /user/updateUser- Update user profilePOST /user/updateUserPass- Update passwordPOST /user/resetUserPass- Reset passwordDELETE /user/deleteUser/:id- Delete user account
POST /restaurant/create- Create restaurant (Auth required)GET /restaurant/getAll- Get all restaurantsGET /restaurant/get/:id- Get restaurant by IDDELETE /restaurant/delete/:id- Delete restaurant (Auth required)
POST /food/create- Create food item (Auth required)GET /food/getAll- Get all food itemsGET /food/get/:id- Get food item by IDGET /food/getByResturant/:id- Get foods by restaurantPUT /food/update/:id- Update food item (Auth required)DELETE /food/delete/:id- Delete food item (Auth required)POST /food/placeorder- Place an order (Auth required)POST /food/orderstatus/:id- Update order status (Auth required)
POST /category/create- Create category (Auth required)GET /category/getall- Get all categoriesGET /category/get/:id- Get category by IDPUT /category/update/:id- Update category (Auth required)DELETE /category/delete/:id- Delete category (Auth required)
- Test endpoints for API health checks
{
username: String (required),
email: String (required, unique),
password: String (required),
address: String (required),
phone: String (required),
usertype: String (enum: ['client', 'admin', 'vendor', 'driver']),
profile: String (default profile image URL)
}{
title: String (required),
imageUrl: String,
foods: Array,
pickup: Boolean (default: true),
delivery: Boolean (default: true),
isOpen: Boolean (default: true),
logoUrl: String,
rating: Number (1-5, default: 1),
ratingCount: String,
code: String,
coords: {
id: String,
latitude: Number,
longitude: Number,
address: String,
title: String
}
}{
title: String (required),
description: String (required),
price: Number (required),
imageUrl: String (default image),
foodTags: String,
category: String,
code: String,
isAvailable: Boolean (default: true),
restaurant: ObjectId (ref: Restaurant),
rating: Number (1-5, default: 5),
ratingCount: String
}{
title: String (required),
imageUrl: String (default image)
}- Client: Regular customers who can browse and order food
- Admin: Full system access and management capabilities
- Vendor: Restaurant owners who can manage their restaurants and food items
- Driver: Delivery personnel for order fulfillment
All API responses follow a consistent format:
Success Response:
{
"success": true,
"message": "Operation successful",
"data": { ... }
}Error Response:
{
"success": false,
"message": "Error description",
"error": "Detailed error message"
}The API includes test routes for health checks. Access them at:
GET /api/v1/test
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
For production deployment:
- Set up environment variables on your hosting platform
- Ensure MongoDB connection is properly configured
- Set
NODE_ENV=production - Use a process manager like PM2 for production