-
Notifications
You must be signed in to change notification settings - Fork 11
API Server
Melissa Stock edited this page May 23, 2019
·
1 revision
- 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){}})
-
Often called pre and post hooks
-
Used with async functions within the schema
-
4 types:
-
- where this = document: validate, save, remove, init
-
- where this = model: insertMany
-
- where this = aggregate: aggregate
-
- 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