Exercise 8: Updating a Resource with PUT

  1. If we wanted to update, say, the first todo list, conventionally, PUT requires us to send the whole updated todo resource. Now let's create a PUT route:
{
method: 'PUT',
path: '/todo/{id}',
handler: (request, reply) => {
const index = request.params.id - 1;
// replace the whole resource with the new one
todoList[index] = request.payload;
return reply({ message: 'updated' });
}
}

Use the todo.js file for your reference at Code/Lesson-2/exercise-c1/routes.
  1. Now go to Insomnia and make the request. Remember to change the request type to PUT:
  1. You should see the following response:
{
"message": "updated"
}

  1. And when you do a GET on http://localhost:8000/todo/1, you should get the updated resource:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset