Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“¦ RESTful Products API

πŸ“Œ About

This project is a RESTful API built with Node.js and Express, focused on applying industry best practices, including:

  • Semantic URIs (plural nouns)
  • Proper HTTP methods
  • Correct status codes
  • Data validation
  • Standardized responses

βš™οΈ Installation

npm init -y
npm install express colors dotenv

▢️ Running the application

node server.js

πŸ”„ Run with auto-reload

node --watch server.js

🌐 Server

http://localhost:3000

πŸ” Environment Variables

Create a .env file in the root:

PORT=3000

πŸ“‘ Headers

All requests must include:

Content-Type: application/json

πŸ“ Project Structure

products-api/
β”œβ”€β”€ utils/
β”‚   └── response.js
β”œβ”€β”€ prints/
β”‚   β”œβ”€β”€ errors/
β”‚   β”‚   β”œβ”€β”€ DELETE-erro.png
β”‚   β”‚   β”œβ”€β”€ GET-404.png
β”‚   β”‚   β”œβ”€β”€ PATCH-erro.png
β”‚   β”‚   β”œβ”€β”€ POST-create-erro.png
β”‚   β”‚   └── PUT-erro.png
β”‚   β”œβ”€β”€ logs/
β”‚   β”‚   └── Logs.png
β”‚   └── sucess/
β”‚       β”œβ”€β”€ DELETE-products.png
β”‚       β”œβ”€β”€ GET-product-id.png
β”‚       β”œβ”€β”€ GET-products.png
β”‚       β”œβ”€β”€ PATCH-product.png
β”‚       β”œβ”€β”€ POST-products.png
β”‚       └── PUT-products.png
β”œβ”€β”€ server.js
β”œβ”€β”€ package.json
└── README.md

πŸ”— Endpoints

Method Route Description
GET /products List all products
GET /products/{id} Get product by ID
POST /products Create a new product
PUT /products/{id} Fully update a product
PATCH /products/{id} Partially update a product
DELETE /products/{id} Delete a product
GET /health API health check

πŸ“₯ Example Request (POST)

{
  "name": "Laptop",
  "price": 5000,
  "stock": 10,
  "category": "Electronics"
}

πŸ“€ Response Pattern

βœ… Success

{
  "status": 200,
  "type": "success",
  "message": "Operation completed successfully",
  "data": {}
}

❌ Error

{
  "status": 404,
  "error": "Not Found",
  "message": "Product not found"
}

⚠️ Validation Error

{
  "status": 400,
  "error": "Bad Request",
  "message": "Name and price must be greater than zero"
}

πŸ“Š Example Responses

GET /products β€” Success (200)

[
  {
    "id": 1,
    "name": "Gaming Laptop",
    "price": 7500,
    "stock": 30,
    "category": "Electronics"
  }
]

GET /products/{id} β€” Not Found (404)

{
  "status": 404,
  "error": "Not Found",
  "message": "Product not found"
}

POST /products β€” Created (201)

{
  "status": 201,
  "type": "success",
  "message": "Product created successfully",
  "data": {
    "id": 3,
    "name": "Mouse",
    "price": 100,
    "stock": 20,
    "category": "Electronics"
  }
}

POST /products β€” Validation Error (400)

{
  "status": 400,
  "error": "Bad Request",
  "message": "Name and price must be greater than zero"
}

πŸ§ͺ Tests

Tested scenarios using Postman:

  • βœ… GET /products β†’ 200
  • ❌ GET /products/{id} β†’ 404
  • βœ… POST valid β†’ 201
  • ❌ POST invalid β†’ 400
  • βœ… PUT β†’ 200
  • βœ… PATCH β†’ 200
  • βœ… DELETE β†’ 204

πŸ“Έ Test Evidence

πŸ–₯️ Logger

Colored logger in terminal


βœ… Success

GET /products β€” List Products (200)

List products

GET /products/{id} β€” Get Product by ID (200)

Get product by id

POST /products β€” Create Product (201)

Create product

DELETE /products/{id} β€” Delete Product (204)

Delete product

PATCH /products/{id} β€” Partially Update Product (200)

Partial update


❌ Errors

GET /products/{id} β€” Product Not Found (404)

Product not found

GET /invalid-route β€” Endpoint Not Found (404)

Route not found


πŸ“ Logger

All requests are logged in the terminal:

GET /products - 200 - 2ms
POST /products - 201 - 0ms
PUT /products/5 - 200 - 1ms
DELETE /products/5 - 200 - 1ms
PATCH /products/2 - 200 - 1ms
GET /aaaaaaa - 404 - 1ms
GET /health - 200 - 1ms
POST /products - 400 - 1ms
PUT /products/5 - 404 - 0ms
GET /products - 200 - 1ms
PUT /products/4 - 400 - 1ms
PUT /products/88 - 404 - 1ms
DELETE /products/78 - 404 - 0ms

πŸ‘¨β€πŸ’» Author

Elyton Moreira

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages