This project is a REST API built using Node.js, Express.js, and MongoDB. It provides CRUD (Create, Read, Update, Delete) operations for managing a collection of products.
- Create a new product
- Read all products
- Update a product by ID
- Delete a product by ID
Before you begin, ensure you have the following installed on your system:
-
Clone this repository:
git clone https://github.com/GLRandula/Rest_API-Node-Express-MongoDB.git cd <repository-folder>
-
Install dependencies:
npm install
-
Configure environment variables: Create a
.env
file in the project root and add the following:PORT=3000 MONGO_URI=mongodb://localhost:27017/your-database-name
-
Start the server:
npm start
The server will run on http://localhost:3000
by default.
http://localhost:3000
- URL:
/create
- Method:
POST
- Description: Add a new product to the database.
- Request Body:
{ "name": "string", "price": "number", "description": "string" }
- Response:
201 Created
on success.400 Bad Request
if the data is invalid.
- URL:
/read
- Method:
GET
- Description: Fetch all products from the database.
- Response:
[ { "_id": "string", "name": "string", "price": "number", "description": "string" } ]
- URL:
/update/:id
- Method:
PUT
- Description: Update an existing product by its ID.
- Request Body:
{ "name": "string", "price": "number", "description": "string" }
- Response:
200 OK
on success.404 Not Found
if the product does not exist.
- URL:
/delete/:id
- Method:
DELETE
- Description: Delete a product by its ID.
- Response:
200 OK
on success.404 Not Found
if the product does not exist.
├── server.js # Entry point for the application
├── routes/
│ └── productRoutes.js # Routes for handling product API requests
├── controllers/
│ └── productController.js # Controller logic for products
├── models/
│ └── productModel.js # Mongoose schema for products
├── .env # MongoDB connection setup
├── package.json # Project dependencies and scripts
└── README.md # Project documentation
- Start Server:
npm start
- Run in Development Mode (with Nodemon):
npm run dev
- Backend Framework: Express.js
- Database: MongoDB (with Mongoose ODM)
- Environment Management: dotenv
The API implements basic error handling to ensure meaningful responses for clients. Common error codes include:
400 Bad Request
: Invalid input data404 Not Found
: Resource not found500 Internal Server Error
: Server issues
Contributions are welcome! If you find any issues or want to add new features:
- Fork the repository
- Create a new branch (
feature/new-feature
) - Commit your changes
- Push to the branch
- Open a pull request
This project is licensed under the MIT License. See the LICENSE
file for details.