Assignment 1 -- REST API with FileSystem Persistence
Project developed using Node.js and Express to manage products and
shopping carts with JSON file storage.
Develop a server that allows:
- Creating, listing, updating, and deleting products.
- Creating carts and adding products to them.
- Persisting data using JSON files (
products.jsonandcarts.json). - Implementing routes using
express.Router(). - Running the server on port 8080.
- Node.js\
- Express\
- FileSystem (
fs)\ - UUID
ENTREGA-1/
│
├── package.json
├── index.js # Application entry point
│
├── src/
│ ├── app.js # Middleware and router configuration
│ │
│ ├── routes/
│ │ ├── products.router.js
│ │ └── carts.router.js
│ │
│ └── data/
│ ├── ProductManager.js
│ ├── CartManager.js
│ ├── products.json
│ └── carts.json
| Method | Route | Description |
|---|---|---|
| GET | / |
Get all products |
| GET | /:pid |
Get a product by ID |
| POST | / |
Create a new product |
| PUT | /:pid |
Update an existing product |
| DELETE | /:pid |
Delete a product by ID |
| Method | Route | Description |
|---|---|---|
| POST | / |
Create a new cart |
| GET | /:cid |
Get products from a specific cart |
| POST | /:cid/product/:pid |
Add a product to a cart (increase quantity if exists) |
Each product has the following structure:
{
"id": "Auto-generated UUID",
"title": "String",
"description": "String",
"code": "String",
"price": 0,
"status": true,
"stock": 0,
"category": "String",
"thumbnails": ["img1.jpg", "img2.jpg"]
}- The
idis automatically generated. - The
idcannot be modified via PUT. - Products are stored in
products.json.
Each cart has the following structure:
{
"id": "Auto-generated UUID",
"products": [
{
"product": "PRODUCT_ID",
"quantity": 1
}
]
}- Only the product ID is stored inside the cart.
- If the same product is added again, the
quantityfield increases. - Carts are stored in
carts.json.
Data persistence is implemented using Node.js fs module.
Data is stored in:
src/data/products.jsonsrc/data/carts.json
Each create, update, or delete operation automatically updates the corresponding JSON file.
-
Clone the repository.
-
Install dependencies:
npm install- Start the server:
npm start- The server runs at:
http://localhost:8080
✔ Server running on port 8080
✔ Express Router implementation
✔ FileSystem persistence
✔ Full Product CRUD
✔ Cart management with quantity increment logic
✔ node_modules excluded from repository