A basic Express API designed to manage products. This API provides CRUD operations for products and is intended to be used with a Next.js frontend.
-
Clone this repository:
git clone https://github.com/dCC-Online/eCommerce_API_Nextjs_Complete
-
Open the unzipped folder in VS Code
-
Install Node Modules:
npm install
-
Start the server:
npm start
The server will start and listen on port 5000 by default. You should see the message:
Server is running on port 5000- Endpoint:
http://localhost:5000/api/products - Method:
GET - Response: Array of products
- Endpoint:
http://localhost:5000/api/products/:id - Method:
GET - Response: Product object
- Endpoint:
http://localhost:5000/api/products - Method:
POST - Body:
{
"name": "Product Name",
"description": "Product Description",
"price": 100.00,
"imageUrl": "online/image/url.jpg"
}- Response:
Created product object
- Endpoint:
http://localhost:5000/api/products/:id - Method:
PUT - Body:
{
"name": "Updated Product Name",
"description": "Updated Product Description",
"price": 150.00,
"imageUrl": "path/to/updated-image.jpg"
}- Response:
Updated product object
- Ensure the server is running using npm start before making requests from your Next.js frontend.
- Example POST Request:
const productData = {
name: "New Product",
description: "This is a new product",
price: 99.99,
imageUrl: "path/to/image.jpg"
};
fetch('http://localhost:5000/api/products', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(productData)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));