-
Create a server route to handle the `GET` request (for all `todos`).
```js app.get('/api/todos', function index(req, res) { // What are you going to send back to the client? }); ```
-
Respond with the collection of `todos` (remember, we don't have a persistent database yet, so we're using an array called `todos` to represent our "database"). It's best practice to respond with an object rather than an array, so create an object with the key `todos`, and the value should be the collection of `todos`.
```js var todos = [ { _id: 1, task: 'Laundry', description: 'Wash clothes' }, { _id: 2, task: 'Grocery Shopping', description: 'Buy dinner for this week' }, { _id: 3, task: 'Homework', description: 'Make this app super awesome!' } ];app.get('/api/todos', function index(req, res) { res.json({ todos: todos }); });
-
Create a server route to handle the `GET` request (for one `todo`).
```js app.get('/api/todos/:id', function show (req, res) { // How would you know which todo is being requested? }); ```
-
Get the todo id from the URL params and save it to a variable.
```js app.get('/api/todos/:id', function show (req, res) { var todoId = parseInt(req.params.id); // How woud you grab the todo with that id? }); ```
-
Use the id to find the todo we want to update (remember, we don't have a persistent database yet, so we're using an array called `todos` to represent our "database"). **Hint:** This is a good opportunity to use filter.
```js app.get('/api/todos/:id', function show(req, res) { var todoId = parseInt(req.params.id);var foundTodo = todos.filter(function (todo) { return todo._id == todoId; })[0];
// What are you going to send back to the client? });
-
Respond with the found todo.
```js app.get('/api/todos/:id', function show(req, res) { var todoId = parseInt(req.params.id);var foundTodo = todos.filter(function (todo) { return todo._id == todoId; })[0];
res.json(foundTodo); });
-
Create a server route to handle the `POST` request.
```js app.post('/api/todos', function create(req, res) { // Where does the data for the new todo live? }); ```
-
Create a new todo with form data (`req.body`).
```js app.post('/api/todos', function create(req, res) { var newTodo = req.body;// How would you "save" the new todo? How do you assign it an _id? });
-
Set a sequential id for the new todo.
```js app.post('/api/todos', function create(req, res) { var newTodo = req.body;if (todos.length > 0) { newTodo._id = todos[todos.length - 1]._id + 1; } else { newTodo._id = 1; }
// How would you "save" the new todo? });
-
Add the new todo to the `todos` array (our "database").
```js app.post('/api/todos', function create(req, res) { var newTodo = req.body;if (todos.length > 0) { newTodo._id = todos[todos.length - 1]._id + 1; } else { newTodo._id = 1; }
todos.push(newTodo);
// What do you send back to the client? });
-
Respond with the new todo.
```js app.post('/api/todos', function create(req, res) { var newTodo = req.body;if (todos.length > 0) { newTodo._id = todos[todos.length - 1]._id + 1; } else { newTodo._id = 1; }
todos.push(newTodo);
res.json(newTodo); });