Question: Your web application is really taking shape. Now you need REST API's that allow UI clients to access your data. Your task this week is
Your web application is really taking shape. Now you need REST API's that allow UI clients to access your data.
Your task this week is to provide new API routes for each of these data methods:
get all items
get a single item
delete an item
add or update an item
Most routes can support a GET request, but your add route will need to support a POST request.
Each API route should:
support crossorigin access,
accept necessary request query parameters or request body,
return appropriate JSON data on success,
return an error status code and message if something went wrong,
Hints
No changes are required to your UI existing data methods, or existing server routes.
You can use Postman Links to an external site. for API testing
See the class notes on crossorigin resource sharing,
Add this command to your index.js file to handle POST requests with a JSON body:
app.useexpressjson; Used to parse JSON bodies
Consider how you will determine whether an item should be added or updated.
MongoDB's insert update and remove methods return a result object that indicates the number of records affected. You can use that to return success or failure information to the client.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
