This project showcases a simple CRUD (Create, Read, Update, Delete) API using Express.js and MongoDB. The API allows you to manage products with fields for name
, quantity
, and price
.
- Node.js installed on your machine
- A MongoDB account (with a connection string)
-
Clone the repository:
git clone https://github.com/avishek999/Crud_expressJs.git cd Crud_expressJs
-
Install dependencies:
npm install
-
Set up MongoDB:
-
Get your MongoDB connection string from your MongoDB account.
-
Create a
.env
file in the root directory and add your MongoDB URI:MONGODB_URI=your_mongodb_connection_string
-
-
Run the server:
npm start
The server will start on
http://localhost:5000
.
-
Create a Product
- Endpoint:
POST /api/products
- Description: Adds a new product to the database.
- Request Body:
{ "name": "Product Name", "quantity": 10, "price": 100 }
- Endpoint:
-
Read All Products
- Endpoint:
GET /api/products
- Description: Fetches all products from the database.
- Endpoint:
-
Read a Single Product
- Endpoint:
GET /api/products/:id
- Description: Fetches a product by its ID.
- Endpoint:
-
Update a Product
- Endpoint:
PUT /api/products/:id
- Description: Updates the details of a product by its ID.
- Request Body:
{ "name": "Updated Product Name", "quantity": 15, "price": 120 }
- Endpoint:
-
Delete a Product
- Endpoint:
DELETE /api/products/:id
- Description: Deletes a product by its ID.
- Endpoint:
To practice CRUD operations, you can build a simple front-end using React or Next.js. Below is a brief guide to help you set up the front-end for testing:
-
Create a new React or Next.js project:
npx create-react-app my-crud-app
or for Next.js:
npx create-next-app my-crud-app
-
Install Axios for making HTTP requests:
npm install axios
-
Create components for each CRUD operation:
CreateProduct.js
for creating a new productProductList.js
for reading all productsProductDetail.js
for reading and updating a single productDeleteProduct.js
for deleting a product
-
Connect your React or Next.js components to the Express.js API endpoints:
- Use Axios to send HTTP requests to the API.
- Handle responses and update the UI accordingly.
This setup provides a foundational understanding of how to create a full-stack application with Express.js, MongoDB, and a front-end framework like React or Next.js. You can extend this project by adding more features or integrating it with other services.
For more details, visit the repository.