Class 9 API Server - Alwynblake/seattle-javascript-401n12 GitHub Wiki
Express()
-
Creates an Express application. The express() function is a top-level function exported by the express module.
-
Var express = require(‘express’);
-
Var app = express();
Application
-
The app object conventionally denotes the Express application. Create it by calling the top-level express()function exported by the Express module:
-
Var expree = require(‘express’);
-
Var app = express();
-
App.get(‘/’, function(req,res){
-
Res.send(‘hello world’); *}):
-
App.listen(3000);
Mongoose
-
Types of Middleware
-
Mongoose has 4 types of middleware: document middleware, model middleware, aggregate middleware, and query middleware. In document middleware functions, this refers to the document.
-
PRE Middlewear Funtions:
-
Are executed one after another, when each middleware calls next. You can use a function that returns a promise. In particular, you can use async/await. If you use next(), the next() call does not stop the rest of the code in your middleware function from executing. Use the early return pattern to prevent the rest of your middleware function from running when you call next().
-
POST Middlewear Functions
-
Are executed after the hooked method and all of its pre middleware have completed. Asynchronous Post Hooks - If your post hook function takes at least 2 parameters, mongoose will assume the second parameter is a next() function that you will call to trigger the next middleware in the sequence. The save() function triggers validate() hooks, because mongoose has a built-in pre('save') hook that calls validate(). This means that all pre('validate') and post('validate') hooks get called before any pre('save') hooks.