Yad2 is a server-side project developed using Node.js. It serves as a system for product mediation with multiple categories, allowing CRUD operations on products and categories. The project includes controllers for managing categories, products, and users, integration with MongoDB for data storage, user authentication using JWT tokens, middleware for user authentication, and CORS support.
To run this project locally, follow these steps:
- Clone the repository:
git clone https://github.com/ShoshiBnayahu/Node.JS-Yad2.git
- Navigate to the project directory:
cd Node.JS-Yad2
- Install dependencies:
npm install
-
Configure MongoDB:
- Ensure MongoDB is installed and running on your machine.
- Update the MongoDB connection URI in the
.envfile if necessary.
-
Start the server:
npm start
-
Category Controller: Supports CRUD operations for categories, including fetching a single category and fetching a sorted list of all categories alphabetically.
-
Product Controller: Allows CRUD operations for products, including fetching all products belonging to a specific category, fetching a specific product from a certain category, updating, and deleting products. Products are returned in sorted arrays.
-
User Module: Includes controllers for user management, supporting login and signup functionalities. User passwords are securely hashed. A unique JWT token is generated upon successful login.
-
Middleware: Utilizes middleware functions for user authentication. All requests, except login, require a valid JWT token attached, verifying the user's identity.
-
Integration with MongoDB: Data is stored and managed using MongoDB, providing a robust and scalable database solution.
-
CORS Support: Enables CORS to allow access from different sources.
-
Database Configuration:
- Ensure MongoDB is installed and running.
- Update the MongoDB connection URI in the
.envfile.
-
Environment Variables:
- Make sure to include all sensitive information, such as database URI, authentication details, and port in the
.envfile.
- Make sure to include all sensitive information, such as database URI, authentication details, and port in the
-
Running the Server:
- Execute
npm startto start the server.
- Execute
Ensure you have a valid JWT token to access restricted endpoints. Regular users have limited access compared to administrators.
GET /api/categories: Fetch all categories sorted alphabetically.GET /api/categories/:id: Fetch a specific category by ID.POST /api/categories: Create a new category (admin only).PUT /api/categories/:id: Update an existing category (admin only).DELETE /api/categories/:id: Delete a category (admin only).
GET /api/products: Fetch all products.GET /api/products/category/:categoryId: Fetch all products belonging to a specific category.GET /api/products/category/:categoryId/:productId: Fetch a specific product from a specific category.POST /api/products: Create a new product (admin only).PUT /api/products/:id: Update an existing product (admin only).DELETE /api/products/:id: Delete a product (admin only).
POST /api/users/login: Login with username and password, returns a JWT token.POST /api/users/signup: Register a new user.
Database credentials and all necessary settings are defined in the .env file, ensuring privacy and security.
DB_URI=mongodb://localhost/Yad2
This project was developed following the specifications provided. The guides and documentation for Node.js, Express.js, and MongoDB were heavily referenced during development.