/express-api/NOTES.md

https://bitbucket.org/petey284/ga-repos · Markdown · 75 lines · 58 code · 17 blank · 0 comment · 0 complexity · bbb6fef094b1406d40679faf03519ff5 MD5 · raw file

  1. - In the config file, you are actually calling all those methods on the routes object. Digging in there you will see what those methods actually do.
  2. - If we want to get really deep:
  3. 1. In `lib/wiring/routes`, all the standard http verbs are being attached as methods to the `routes` object.
  4. 2. These methods result in a method being called on the `router` object that matches that http verb, with the arguments supplied by calling the controller for that action.
  5. 3. When the server starts up, all the methods that you see in `config/routes.js` are being called on the routes object being exported from `lib/wiring/routes`.
  6. 4. The `resources` method will add the standard REST actions to a resource for us, we can call this on additional resources in `config/routes.js`.
  7. - The order in which your route handlers and other middleware functions are declared is the order which they will be executed.
  8. ### Annotate Along: Index Action
  9. - What is the purpose of this action?
  10. - To get all of the resources for this route
  11. - Does it need a singular or plural resource to build its response?
  12. - A singular resource
  13. - How is the action handling errors?
  14. - With the `.catch` statement at the end
  15. - Why do we need to check for the existence of a record after querying?
  16. - Where do we get IDs from?
  17. - We get our IDs from Mongo automatically assigning them after a `create`
  18. - Where do we get data from when creating or updating a record?
  19. - The data should be contained in the request object
  20. - Which terminal handler is used to send a response?
  21. - In this case, we are using `res.json`
  22. - `res.json` is an **express** method
  23. - This is a terminal handler that signifies to the server that the
  24. connection created to modifiy the response can be closed. Failure to use a
  25. terminal handler will result in a hanging connection.
  26. - `.toJSON` is a **mongoose** method which accepts options.
  27. - Instead of applying these options on a per-document basis we may declare
  28. the options in this method and have it applied to all of the schema's
  29. documents by default.
  30. - `e.toJSON({ virtuals: true, user: req.user }))`
  31. - Transform method for example model is ran passing in virtuals and user as
  32. arguments
  33. - In `app/controllers/examples.js`, what is `Object.assign` doing?
  34. - Basically, this is a way to copy new key-value pairs onto a target object.
  35. ### Demo: An Example Express Model
  36. - What library are we using to model our resources? Does it have anything to do with Express?
  37. - Looks like we're using Mongoose - it does not
  38. - What does the underscore denote in _owner?
  39. - This indicates that we do not want to be able to change this field
  40. - Where should we go to find out more about an owner?
  41. - Because it references a 'User' model, `models/user.js`
  42. - Why aren't we using an arrow function for the virtual attribute length?
  43. - Because `this` binding changes inside an arrow function, and we want `this` to be pointing to `exampleSchema`. From the docs: "Virtual attributes are attributes
  44. that are convenient to have around but that do not get persisted to mongodb." In other words, we are attaching a temporary attribute to our data that can be accessed by calling `.length`.
  45. ### Code-Along: GET /books
  46. For this one, basically just copy `controllers/examples.js` into `controllers/books.js` and replace the word "Example" with "Book". Note: Be careful here, use command-d to find all the instances of "example" (small e) and "Example" (big e).
  47. Show why it fails. Edit the `config` file to include the books resource. Show why it fails. Finally, edit the Books model and see success. Note that we don't have to include the virtual at the end of the example model.
  48. ### Code-Along: Add Books to the database
  49. First we have to create a user, and save the ID. Something like this will do:
  50. - `EMAIL=pikachu PASSWORD=pikachu sh scripts/auth/sign-up.sh`
  51. Once we have the User ID, include that in the load-books script:
  52. - `node bin/load-books.js <User-ID-here>`
  53. ### Lab: Create an Example
  54. Here the trick is for them to enter a TOKEN and TEXT argument in the command line before running the script. They can use the token they generated earlier. For the `show` script, they will instead need a TOKEN and and ID from the example they previously created.
  55. ### Code-Along: GET /books/:id
  56. The code for this is already there since we copied over from `controllers/examples.js`. All we really need to do is copy in the test script, and find and ID from when we got all the boks earlier to set as an argument. The remaining code-alongs will follow this basic structure.
  57. ### Code-Along: PATCH /books/:id
  58. For this one, feel free to show them that you don't have to update `title`. By looking at the model, we can choose any field we want to update. `author`, for example.