This backend service manages products and users in a microservices architecture, utilizing MongoDB for storage. The service is built with Node.js and Express, and it provides RESTful APIs for creating, reading, updating, and deleting products and users.
-
Clone the repository:
git clone https://github.com/yourusername/products-users-backend.git cd products-users-backend
-
Install dependencies:
npm install
-
Set up environment variables:
Create a
.env
file in the root directory and add the following variables:MONGODB_URI=mongodb://localhost:27017/yourdbname PORT=3000 JWT_SECRET=your_jwt_secret CLOUDINARY_CLOUD_NAME=your_cloud_name CLOUDINARY_API_KEY=your_api_key CLOUDINARY_API_SECRET=your_api_secret
-
Start the service:
npm start
After starting the service, it will be available at http://localhost:3000
. Use a tool like Postman to interact with the API endpoints.
-
URL:
/product/new
-
Method:
POST
-
Request Body:
{ "name": "Product1", "description": "A sample product", "price": 29.99, "stock": 10, "category": "Category1", "images": [ { "url": "images.com" } ] }
-
Response:
{ "id": "60c72b2f9b1d4b3d5c4a627a", "name": "Product1", "description": "A sample product", "price": 29.99, "category": "Category1", "createdAt": "2024-05-21T14:09:55.418Z", "updatedAt": "2024-05-21T14:09:55.418Z" }
-
URL:
/products
-
Method:
GET
-
Response:
[ { "id": "60c72b2f9b1d4b3d5c4a627a", "name": "Product1", "description": "A sample product", "price": 29.99, "category": "Category1", "createdAt": "2024-05-21T14:09:55.418Z", "updatedAt": "2024-05-21T14:09:55.418Z" } ]
-
URL:
/products/:productId
-
Method:
GET
-
Response:
{ "id": "60c72b2f9b1d4b3d5c4a627a", "name": "Product1", "description": "A sample product", "price": 29.99, "category": "Category1", "createdAt": "2024-05-21T14:09:55.418Z", "updatedAt": "2024-05-21T14:09:55.418Z" }
-
URL:
/products/:productId
-
Method:
PUT
-
Request Body:
{ "name": "Updated Product", "description": "Updated description", "price": 39.99, "category": "Updated Category" }
-
Response:
{ "id": "60c72b2f9b1d4b3d5c4a627a", "name": "Updated Product", "description": "Updated description", "price": 39.99, "category": "Updated Category", "createdAt": "2024-05-21T14:09:55.418Z", "updatedAt": "2024-05-21T14:09:55.418Z" }
-
URL:
/products/:productId
-
Method:
DELETE
-
Response:
{ "message": "Product deleted successfully" }
-
URL:
/users
-
Method:
POST
-
Request Body:
{ "username": "John Doe", "email": "john.doe@example.com", "password": "password123" }
-
Response:
{ "id": "60c72b2f9b1d4b3d5c4a627b", "username": "John Doe", "email": "john.doe@example.com", "createdAt": "2024-05-21T14:09:55.418Z", "updatedAt": "2024-05-21T14:09:55.418Z" }
-
URL:
/users
-
Method:
GET
-
Response:
[ { "id": "60c72b2f9b1d4b3d5c4a627b", "username": "John Doe", "email": "john.doe@example.com", "createdAt": "2024-05-21T14:09:55.418Z", "updatedAt": "2024-05-21T14:09:55.418Z" } ]
-
URL:
/users/:userId
-
Method:
GET
-
Response:
{ "id": "60c72b2f9b1d4b3d5c4a627b", "name": "John Doe", "email": "john.doe@example.com", "createdAt": "2024-05-21T14:09:55.418Z", "updatedAt": "2024-05-21T14:09:55.418Z" }
-
URL:
/users/:userId
-
Method:
PUT
-
Request Body:
{ "name": "Updated User", "email": "updated.email@example.com", "password": "newpassword123" }
-
Response:
{ "id": "60c72b2f9b1d4b3d5c4a627b", "name": "Updated User", "email": "updated.email@example.com", "createdAt": "2024-05-21T14:09:55.418Z", "updatedAt": "2024-05-21T14:09:55.418Z" }
-
URL:
/users/:userId
-
Method:
DELETE
-
Response:
{ "message": "User deleted successfully" }
- Node.js: JavaScript runtime built on Chrome's V8 JavaScript engine.
- Express: Fast, unopinionated, minimalist web framework for Node.js.
- MongoDB: NoSQL database for storing product and user data.
- Mongoose: Elegant MongoDB object modeling for Node.js.
- JWT (JSON Web Tokens): For authentication and authorization.
- Cloudinary: Cloud-based image and video management services.
This service uses JWT (JSON Web Tokens) for authentication and authorization. Ensure you have a valid token to access the protected endpoints.
Contributions are welcome! Please follow these steps:
-
Fork the repository.
-
Create a new branch:
git checkout -b feature-branch
-
Make your changes.
-
Commit your changes:
git commit -m 'Add some feature'
-
Push to the branch:
git push origin feature-branch
-
Open a pull request.
Please ensure your code adheres to the project's coding standards and includes appropriate tests.
This project is licensed under the MIT License. See the LICENSE file for details.
- Add unit tests to increase the reliability of the API.
- Create integration tests to validate API behavior in different scenarios.
- Utilize linter and formatter tools for code cleanliness.
- Regularly update dependencies and stay informed about security vulnerabilities.