CPNT 262 Day 20 - Introduction to Mongoose and Heroku with Atlas
Housekeeping
- Proposed assignment changes
- Assignment 6 removed
- Assignment 5 upgraded to include Mongoose
- Weight: 20%
- Due: Wednesday Nov 10
- Group project now 40% (15% for individual contribution)
- Tony needs your team names
- Ash will be covering for Tony this afternoon
Trophy of the Day
- Have ONE member submit a group charter as a Daily
1. express.Router
Materials
- ExpressJS - Routing on Tutorials Point
- Reference: Express Routes -
express.Router
Demo: Guild routes with express.Router
Key Takeaways
Route modules work much like other local modules but require some added steps:
-
Inside the route module file (
./routes/api.js
):-
express
is required as a module. -
Your router is defined using the
express.Router
function:const express = require('express')
const router = express.Router()- it's convention to name the app
router
when within a local module.
- it's convention to name the app
-
Route handlers operate the same, except:
- the
router.get()
is used instead ofapp.get()
; - the path does not include
/api
because that is assigned inserver.js
below.
- the
-
router
is exported normally:module.exports = router
-
-
In
server.js
:-
The route module is required normally:
const api = require('./routes/api')
-
The often missed step: attach your route to a path using middleware (
app.use()
) and the optional path argument:app.use('/api', api)
-
Move any modules that the new router depends on, such as the
guild.js
data and therandom-item.js
module in the Guild example.
-
2. Introduction to Mongoose
Materials
- Mongoose CRUD (Create, Read, Update, Delete)
- Mongoose Getting Started (kittens!)
- Gist: Read MongoDB data with Mongoose and Express
Demos
- Kittens in Mongoose Getting Started
- If there's time: Guild app with mongoose
Key Takeaways
- MongoDB is a file database engine. It literally stores its data as a folder (i.e. Collection) of
json
files (i.e. Documents). - Working with MongoDB is a little messy so we use the
mongoose
package to:- use a simpler syntax;
- ensure the documents in a collection have the same structure;
- hide the gritty details like promises and asynchronous things.
3. MongoDB Atlas on Heroku
Materials
Key Takeaways
- Most of the steps in this tutorial were completed yesterday so you should only have to complete the following to have your Heroku connect to Atlas:
- Add your connection string to the Config Var section of your App Settings.
- Note: CORS is probably not needed for your app to operate.
Lab Time
- Have ONE member submit a group charter as a Daily
- Assign the key roles and responsibilities for each group member in delivering the Final Project. After each entry write a short summary defining their role and what their responsibilities are in the project.