API Server - 401-advanced-javascript-davetrost/alchemy-fsjs-fall-2019 GitHub Wiki

API Server

Reading Materials

Express router.params middleware

The router.params() function adds callback triggers to route parameters. Primarily, the use case that I found interesting was the example of a user id being provided in the URL path. A call to router.params is able to intercept the user id and to translate that into a user model (from the DB) and attach that information to the request object.

Mongoose Middleware

Background on Documents and Collections in Mongoose:

  • Documents
    • ‘Documents’ in MongoDB are equivalent to records or rows of data in SQL. While an SQL row can reference data in other tables, Mongo documents usually combine that in a single document.
  • Collections
    • ‘Collections’ in Mongo are equivalent to tables in relational databases. They can hold multiple JSON documents.

Mongoose has 4 types of middleware:

  • Document Middleware
    • validate
    • save
    • remove
    • init
  • Collection Middleware
    • count
    • deleteMany, deleteOne
    • find, findOne, findOneAndDelete, findOneAndRemove, findOneAndUpdate
    • remove
    • update, updateOne, updateMany
  • Aggregate middleware
    • This middleware is for MyModel.aggregate(). Aggregate middleware executes when you call exec() on an aggregate object.
    • aggregate
  • Model middleware
    • insertMany