Building the Todos index API request

First, back in server.js, we'll need to put together our list of Todo items. You'll likely recognize these from the Todo items that we've used previously, with the exception of the fact that we now also have some id in the items as well. This is a closer representation to what we'd see normally in a server, where the items would not only contain their normal attributes but also form of primary keys as well. We'll store these as a const since we don't want to be able to accidentally overwrite them later:

const todos = [
{ id: 1, description: 'Write some code', done: false, critical: false },
{ id: 2, description: 'Change the world', done: false, critical: false },
{ id: 3, description: 'Eat a cookie', done: false, critical: false }
];

To get these Todos out via the server, we'll need to create a new route to an endpoint, /api/todos:

app.get('/api/todos', (req, res) => res.json({ todos: todos }));

First, you'll need to start up the backend using yarn backend. Next, if we send a request via some network tool to send HTTP requests (for example, using Postman), we should be able to verify the results by making a GET request to http://localhost:4000/api/todos:

We can also delete the app.get("/"...) line, since we no longer require that code.

There are a lot of ways to simulate requests to your backend. Similar to choices on code editors, Postman is my tool of choice for manually sending HTTP requests to verify the results, or you may already have tools available to you, such as CURL! Whatever you prefer to use should work just fine.
..................Content has been hidden....................

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