Guided project for Node API Module.
In this project we will learn how to create a simple Web API using Node.js
and Express
, and cover the basics of server-side routing using global middleware.
- Clone this repository.
- Open the project folder in VSCode.
Build a RESTful Web API for an online store to Create, Read, Update and Delete "Products" .
A Product has:
- a unique
id
. - a
title
. - a
price
. -
- a
description
.
- a
-
- a
category
.
- a
-
- an
image
.
- an
-
- a
rating
.
- a
The Web API must provide a set of endpoints
to fulfill the following needs:
- add a new Product.
- view a list of existing Products.
- view the details of a single Product.
- update the information of an existing Product.
- remove a Product.
Here is a table with the endpoint
descriptions:
Action | URL | Method | Response |
---|---|---|---|
Add a Product | /api/products | POST | the new Product |
View list of Products | /api/products | GET | array of Products |
View Product details | /api/products/{id} | GET | a Product |
Update Product | /api/products/{id} | PUT | updated Product |
Remove a Product | /api/products/{id} | DELETE | deleted Product |
- If there's an error in retrieving the products from the database:
- respond with HTTP status code
500
. - return the following JSON:
{ message: "The products information could not be retrieved" }
.
- respond with HTTP status code
-
If the product with the specified
id
is not found:- return HTTP status code
404
(Not Found). - return the following JSON:
{ message: "The product with the specified ID does not exist" }
.
- return HTTP status code
-
If there's an error in retrieving the product from the database:
- respond with HTTP status code
500
. - return the following JSON:
{ message: "The product information could not be retrieved" }
.
- respond with HTTP status code
-
If the request body is missing the
title
ordescription
or any other required property:- respond with HTTP status code
400
(Bad Request). - return the following JSON:
{ message: "Please provide the required fields for the product." }
.
- respond with HTTP status code
-
If the information about the product is valid:
- save the new product the the database.
- return HTTP status code
201
(Created). - return the newly created product.
-
If there's an error while saving the product:
- respond with HTTP status code
500
(Server Error). - return the following JSON:
{ message: "There was an error while saving the product to the database" }
.
- respond with HTTP status code
-
If the product with the specified
id
is not found:- return HTTP status code
404
(Not Found). - return the following JSON:
{ message: "The product with the specified ID does not exist" }
.
- return HTTP status code
-
If the request body is missing the
title
ordescription
or any other required property:- respond with HTTP status code
400
(Bad Request). - return the following JSON:
{ message: "Please provide the required fields for the product." }
.
- respond with HTTP status code
-
If there's an error when updating the product:
- respond with HTTP status code
500
. - return the following JSON:
{ message: "The product information could not be modified" }
.
- respond with HTTP status code
-
If the product is found and the new information is valid:
- update the product document in the database using the new information sent in the
request body
. - return HTTP status code
200
(OK). - return the newly updated product.
- update the product document in the database using the new information sent in the
-
If the product with the specified
id
is not found:- return HTTP status code
404
(Not Found). - return the following JSON:
{ message: "The product with the specified ID does not exist" }
.
- return HTTP status code
-
If there's an error in removing the product from the database:
- respond with HTTP status code
500
. - return the following JSON:
{ message: "The product could not be removed" }
.
- respond with HTTP status code