Exercise 6: Getting a Specific Resource

  1. Now, let's try and get a specific todo. Since we don't have a database with IDs, we will take the indices to be IDs, [0] being 1, and so on.

Use the exercise-c1 folder for your reference at Code/Lesson-2.
  1. Let's add a route for that. Notice that we use {<parameter-key>} as a way of passing request parameters to our route function, then get it through request.params.id:
module.exports = [
{
method: 'GET',
path: '/todo',
...
handler: (request, reply) => {
const id = request.params.id - 1;
// since array is 0-based index
return reply(todoList[id]);
}
},
];

Use the todo.js file for your reference at Code/Lesson-2/exercise-c1/routes.
  1. Go to Insomnia and do a GET request to http://localhost:8000/todo/1. You should see this:
..................Content has been hidden....................

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