Question: For the DELETE method, items are deleted by index, i.e., the query string will hold the index of the item to be deleted within
For the DELETE method, items are deleted by index, i.e., the query string will hold the index of the item to be deleted within the list. Note that the index starts from 0. 1. Create a new handler for the DELETE method. Call the handler "DELETEhandler". Like the POST handler, pass the file name, the query string object, and the callback function. 2. Retrieve the list from the file using the helper function. 3. Check that the index passed in the query string exists in the list. i.e., the index to be deleted must be less than the length of the list a. If the index is equal or greater than the length of the list, then return the status code '404' and message 'NOT FOUND'. b. 4. In the server handler, call the DELETE handler and pass to it the json file, the query string, and the callback function that will return the status code and message. 5. Fill the response header with the status code, and content type (check the code from the POST handler above). 6. Send the response with the message returned from the handler (check the code from the POST handler above). 7. Update the code for the invalid path to return an appropriate status code and message as before 8. Save your code and run the server 9. Using Postman, check the correct operation of the server for both paths a. Send a request to delete an item at an index that is not in the list. b. Send a request to delete an item in the list and use the GET method to display the list after deletion. Otherwise, remove the item at the given index from the list and store the list back in the file. For the PUT method, items to be updated are identified by their index, i.e., the query string will hold the index of the item in addition to the field(s) to be updated. Note that the index starts from 0 and all fields for the todo list and shopping list can be updated. 1. Create a new handler for the PUT method. Call the handler "PUTHandler". Like the POST handler, pass to the handler the file name, the query string object, and the callback function. 2. In the handler, retrieve the list from the json file using the helper function. 3. Check that the index passed in the query string exists in the list. i.e., the index to be updated must be less than the length of the list a. If the index is equal or greater than the length of the list, then return the status code '404' and message 'NOT FOUND'. b. Otherwise, get the name of the keys in the query string object and update the relevant fields in the retrieved item (Hint: check Object.keys() static method and indexOf() method of Array instances in JS). Note that you will use a direct assignment statement to update the field. 4. In the routing logic for the PUT method, call the PUT handler passing to it the corresponding json file, the query string, and the callback function. The call back function has two parameters that will return the status code and corresponding message from the PUT handler. 5. Fill the response header with the status code, and content type (check the code from the POST handler above). 6. Send the response with the message returned from the handler (check the code from the POST handler above). 7. Update the code for the invalid path to return an appropriate status code and message as before.
Step by Step Solution
3.59 Rating (160 Votes )
There are 3 Steps involved in it
Heres an example implementation of the DELETE and PUT handlers based on the instructions provided javascript const fs requirefs const url requireurl H... View full answer
Get step-by-step solutions from verified subject matter experts
