Skip to content

Latest commit

 

History

History
168 lines (135 loc) · 4.96 KB

cheat_sheet.md

File metadata and controls

168 lines (135 loc) · 4.96 KB

Todo API: Step by Step - Create & Read Routes

todos#index

  1. 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? }); ```
  1. 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 }); });

    
    

todos#show

  1. 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? }); ```
  1. 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? }); ```
  1. 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? });

  1. 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); });

todos#create

  1. 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? }); ```
  1. 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? });

  1. 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? });

  1. 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? });

  1. 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); });

Resources

Testing Resources

  • Mocha - framework for running tests
  • Chai (expect) - for expect assertions
  • Request - for handling HTTP requests and responses