This project is a simple CRUD (Create, Read, Update, Delete) API built with Express.js and MongoDB using Mongoose for data modeling. The application allows you to perform basic operations on a Product
resource.
- Create: Add new products to the database.
- Read: Retrieve a list of all products or get details of a specific product by ID.
- Update: Modify details of an existing product by ID.
- Delete: Remove a product from the database by ID.
- Express.js: Web framework for Node.js.
- MongoDB: NoSQL database for storing data.
- Mongoose: ODM (Object Data Modeling) library for MongoDB and Node.js.
- Node.js and npm installed on your machine.
- MongoDB database (either local or MongoDB Atlas).
-
Clone the repository:
git clone https://github.com/fattahsamit/products-crud-backend.git
-
Navigate to the project directory:
cd crud-app
-
Install dependencies:
npm install
-
Create a .env file in the root directory of the project and add your MongoDB connection details:
PORT=8000 DB_USERNAME=mongodb-username DB_PASSWORD=mongodb-password
-
Start the server:
npm start
-
Run the server in development mode with auto-reloading:
npm run dev
- GET /api/products: Retrieve all products. Optionally, add a limit query parameter to limit the number of results.
- GET /api/products/:id: Retrieve a single product by ID.
- POST /api/products: Create a new product. Requires a JSON body with product details.
- PUT /api/products/:id: Update an existing product by ID. Requires a JSON body with updated product details.
- DELETE /api/products/:id: Update an existing product by ID. Requires a JSON body with updated product details.
To test the API endpoints with Postman, you can use the provided Postman collection. Follow these steps to import the collection:
- Open Postman
- Import the Postman Collection, use the
JSON
file provided in the testing folder - Use the Collection:
- Once imported, you will find the collection in the left sidebar under Collections.
- Expand the collection to see the available requests.
- Click on a request to view and send it.
- Modify the request details if needed (e.g., change the endpoint URL or add query parameters).
To test the API endpoints with Postman, you can use the provided Postman collection. Follow these step
-
Get All Products with a Limit:
- Select the GET request for /api/products.
- Add a query parameter limit with a value (e.g. 5).
- Click Send to see the response.
-
Post a New Product:
- Select the POST request for /api/products.
- In the Body tab, select raw and choose JSON from the dropdown.
- Enter the product details in JSON format, for example:
{ "name": "New Product", "quantity": 15, "price": 120 }
- Click Send to create the new product.
You can add test scripts in Postman to automatically validate responses. Here is an example of test scripts that you can include in Postman for automated testing of API responses:
Example Test Scripts:
- Test for POST /api/products:
pm.test("Status code is 201", () => {
pm.response.to.have.status(201);
});
const response = pm.response.json();
const product = response._id;
pm.collectionVariables.set("product", product);
Running Tests in Postman:
- Test scripts are already added. Data can be modified, click the
Send
button on any request, and Postman will automatically run the tests after receiving a response. - The results of the tests will be displayed under the
Tests
tab of the response panel.
This project is licensed under the MIT License. See the LICENSE file for details.
- Search: Enable keyword search for products.
- Sorting: Implement sorting by attributes like price or name.
- Rate Limiting: Prevent abuse with rate limiting.
- Caching: Use caching to improve performance and reduce database load.
- API Documentation: Provide interactive API documentation with Swagger.
Fattah Samit - https://github.com/fattahsamit
This README.md
provides a comprehensive overview of your project, installation steps, usage instructions, and additional details to help users get started with your CRUD application.