CPNT 262 Day 20 - Introduction to Mongoose and Heroku with Atlas

Housekeeping

  • Proposed assignment changes
    1. Assignment 6 removed
    2. Assignment 5 upgraded to include Mongoose
      • Weight: 20%
      • Due: Wednesday Nov 10
    3. 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

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):

    1. express is required as a module.

    2. 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.
    3. Route handlers operate the same, except:

      • the router.get() is used instead of app.get();
      • the path does not include /api because that is assigned in server.js below.
    4. router is exported normally:

      module.exports = router
  • In server.js:

    1. The route module is required normally:

      const api = require('./routes/api')
    2. The often missed step: attach your route to a path using middleware (app.use()) and the optional path argument:

      app.use('/api', api)
    3. Move any modules that the new router depends on, such as the guild.js data and the random-item.js module in the Guild example.


2. Introduction to Mongoose

Materials

Demos

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.