This is a simple demo API project built with Node.js, Express, and MongoDB. It provides basic Create and Read operations for managing products. The project structure is organized into controllers, models, routes, and utility functions.
.
├── index.js
├── package.json
├── pnpm-lock.yaml
└── src
├── controllers
│ └── product.controllers.js
├── models
│ └── product.model.js
├── routes
│ └── product.routes.js
└── utils
├── connectDB.js
└── handlers.js
-
index.js: The main entry point of the application, where the server is configured and started.
-
package.json: Configuration file for Node.js project, including dependencies and scripts.
-
pnpm-lock.yaml: Lock file for package manager (PNPM, in this case).
-
src: Source code directory containing controllers, models, routes, and utility functions.
Before running the project, ensure you have the following installed:
-
Clone the repository:
git clone <repository-url>
-
Install dependencies:
pnpm install
-
Set up environment variables:
Create a
.env
file in the root directory and add the following:PORT=8000 MONGO_URI=mongodb://localhost:27017/demo-apis
Adjust the
MONGO_URI
to your MongoDB connection string. -
Run the application:
pnpm run dev
This command uses
nodemon
for development, which automatically restarts the server when changes are detected.
- Endpoint:
/api
- Method: GET
- Description: Display a welcome message.
- Endpoint:
/api/v1/products
- Method: GET
- Description: Fetch all products.
-
Endpoint:
/api/v1/products
-
Method: POST
-
Description: Create a new product.
-
Request Body:
{ "name": "Product Name", "price": 19.99, "description": "Product Description", "countInStock": 10, "imageUrl": "https://example.com/image.jpg" }
You can use tools like Postman or curl to test the endpoints.
-
Open Postman or use curl commands to interact with the API.
-
Use the provided endpoints and methods to test various operations.
- Endpoint:
GET http://localhost:8000/api
- Method: GET
{
"message": "Welcome to the API"
}
- Endpoint:
GET http://localhost:8000/api/v1/products
- Method: GET
{
"message": "Product fetched successfully",
"data": [
{
"_id": "product_id_1",
"name": "Product 1",
"price": 19.99,
"description": "Product 1 Description",
"countInStock": 10,
"imageUrl": "https://example.com/product1.jpg",
"createdAt": "2023-01-01T12:00:00Z",
"updatedAt": "2023-01-01T12:30:00Z"
},
{
"_id": "product_id_2",
"name": "Product 2",
"price": 29.99,
"description": "Product 2 Description",
"countInStock": 5,
"imageUrl": "https://example.com/product2.jpg",
"createdAt": "2023-01-02T10:00:00Z",
"updatedAt": "2023-01-02T10:45:00Z"
}
]
}
- Endpoint:
POST http://localhost:8000/api/v1/products
- Method: POST
- Request Body:
{ "name": "New Product", "price": 39.99, "description": "New Product Description", "countInStock": 15, "imageUrl": "https://example.com/newproduct.jpg" }
{
"message": "Product created successfully",
"data": {
"_id": "new_product_id",
"name": "New Product",
"price": 39.99,
"description": "New Product Description",
"countInStock": 15,
"imageUrl": "https://example.com/newproduct.jpg",
"createdAt": "2023-01-03T08:00:00Z",
"updatedAt": "2023-01-03T08:15:00Z"
}
}
These examples assume you have the server running locally on http://localhost:8000
. Adjust the base URL accordingly if your server is hosted elsewhere.
Remember to replace placeholder values (e.g., product_id_1, new_product_id) with actual values from your API responses. These examples demonstrate how to interact with your API using different endpoints and methods in Postman.
- The project uses
dotenv
for environment variable management. - Validation of request bodies is handled using
joi
. - MongoDB is used as the database, and the connection is established using Mongoose.
- Error handling is centralized using utility functions in
handlers.js
.