Skip to content

API Server

Melissa Stock edited this page May 23, 2019 · 1 revision

router.param(name, callback)

  • callback(req, res, next, id)
  • Can be used to validate parameters on routes (ie: user data) and adding it to the req
  • We can customize the function signature by in putting a function. router.param(fn(){ return fn(req, res, next, val){}})

Mongoose Middleware

  • Often called pre and post hooks

  • Used with async functions within the schema

  • 4 types:

    1. where this = document: validate, save, remove, init
    1. where this = model: insertMany
    1. where this = aggregate: aggregate
    1. where this = query: count, deleteMany, deleteOne, find, findOne, findOneAndDelete, findOneAndRemove, findOneAndUpdate, remove, update, updateOne, updateMany
  • Pre middleware operates one after another - defined by the flow of 'next'

  • you can use async/await to return promises (this is a better way to control flow compared to 'next')

  • Post middleware operates after the pre middleware and hooked method have completed

Clone this wiki locally