- 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.
- 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.
- Go to Insomnia and do a GET request to http://localhost:8000/todo/1 ...