Exercise 7: Creating a New Todo with POST

  1. Now let's add a new todo. This is where POST comes in. A POST request should always come with a payload which is the data that is being posted. We will add a new route to handle this:
module.exports = [
// previous code
{
method: 'POST',
path: '/todo',
handler: (request, reply) => {
const todo = request.payload;
todoList.push(todo);
return reply({ message: 'created' });

];

Use the todo.js file for your reference at Code/Lesson-2/exercise-c1/routes.
  1. On Insomnia:
    1. Change the request type to POST:
    1. Change the request body to JSON:

    1. Add the request body and the URL appropriately:
  1. When you post the request, you should see this as the response:
{
"message": "created"
}
  1. Now, when you do a GET request to http://localhost:8000/todo, you should see the newly created todo appear as part of the response:
[
...
{
"title": "Languages to Learn",
"dateCreated": "Mar 2, 2018",
    "list": 
[
"C++",
"JavaScript"
]
}
]
..................Content has been hidden....................

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