This is a simple RESTful API built with Node.js and TypeScript. It provides basic CRUD operations for managing products and includes middleware for logging incoming requests.
To run the server locally, follow these steps:
-
Make sure you have Node.js installed. If not, you can download it here.
-
Clone this repository:
-
Navigate to the project directory:
cd products-api
-
Install dependencies:
npm install
-
Start the server:
npm start
By default, the server will run on http://localhost:3000
.
Retrieve a list of products.
-
Endpoint:
GET /api/products
-
Example Response:
[ { "id": 1, "name": "Product 1", "description": "Description 1", "price": 10.99 }, { "id": 2, "name": "Product 2", "description": "Description 2", "price": 19.99 }, { "id": 3, "name": "Product 3", "description": "Description 3", "price": 5.99 } ]
Retrieve a single product by ID.
-
Endpoint:
GET /api/products/:id
-
Example Response:
{ "id": 1, "name": "Product 1", "description": "Description 1", "price": 10.99 }
Create a new product.
-
Endpoint:
POST /api/products
-
Example Request Body:
{ "name": "Product 4", "description": "Description 4", "price": 15.99 }
-
Example Response:
{ "id": 4, "name": "Product 4", "description": "Description 4", "price": 15.99 }
Update an existing product.
-
Endpoint:
PUT /api/products/:id
-
Example Request Body:
{ "name": "Product 4", "description": "Description 4", "price": 15.99 }
-
Example Response:
{ "id": 4, "name": "Product 4", "description": "Description 4", "price": 15.99 }
Delete an existing product.
-
Endpoint:
DELETE /api/products/:id
-
Example Response:
{ "id": 4, "name": "Product 4", "description": "Description 4", "price": 15.99 }
A middleware function is implemented to log the timestamp, HTTP method, and requested URL for every incoming request.
To run the tests, follow these steps:
-
Navigate to the project directory:
cd return-journey-assignment/task4-7
-
Run the tests:
npm test