-
Create an Express app.
- Make a new directory called
expressTrain. - Navigate inside the
expressTrainfolder. - Create an
app.jsfile in the terminal usingtouch. - Initialize a Node.js project in the terminal using
npm init -y. - Open the folder.
- Install Express and Morgan as dependencies using
npm iand require them in theapp.jsfile. - Create a server using Express and use Morgan as a logger. Don't forget to use
.json().
- Make a new directory called
Create an Express server with the following features:
- Implement a global store that contains product objects in the following format:
[ { name: 'apple', price: 1.5 } ]
-
Root Route ("/"):
- Return everything in the store.
-
Get All Products Route ("/get-all-products"):
- Return a list of all product names in an array.
-
Get Product by Name Route ("/get-product/:productName"):
- Return the full object with name and price for the specified product.
-
Create Product Route ("/create-product"):
- Add a new object to the store and return "Product added".
-
Implement additional checks:
- Check if the product already exists before adding.
- If the product exists, return an error message in the response indicating that the item already exists.
-
Implement a delete route for deleting an item by name:
- Remove the specified product from the store.