Exercise 5: Getting a List of Resources

  1. Let's go back to the project from Exercise 4: Setting up the Logger.

Use the exercise-c1 folder for your reference at Code/Lesson-2.
  1. Since we are going to have various routes, it would be prudent to now split our routes to a separate file for the sake of organization. Within the project, create a subfolder called routes.
  2. Inside the created folder, create a file called todo.js. In todo.js, this is where we are going to have all our routes for the todo resource. This file (module) will export a list of routes.
  3. Let's start by doing a simple route that returns a list of todos on a GET request:
const todoList = [
{
title: 'Shopping',
dateCreated: 'Jan 21, 2018',
list: [
{
text: 'Node.js Books', done: false },
...
]
},
{
];

Use the todo.js file for your reference at Code/Lesson-2/exercise-c1/routes.
  1. We then go back to our server.js file, require the todo route module, and register it with our server using the server.route method:
const routes = {};
routes.todo = require('./routes/todo')
// create a server with a host and port
const server = new Hapi.Server();
server.connection(
{
host: 'localhost',
port: process.argv[2] || 8000,
});
server.route(routes.todo);

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

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