Dev dependencies (nodemon) and express.Router()

Homework

  1. Review
  2. nodemon
  3. dotenv
  4. express.Router

Morning reflection

Housekeeping


1. nodemon and dotenv

Learning Objectives

  • Understand the purpose of development dependencies
    • Install nodemon as a development dependency.
    • Add a dev: "nodemon server.js" script to package.json.
    • Start a server using nodemon.
  • Understand the purpose of environment variables.
    • Install dotenv as a dependency.
    • Assign a PORT environment variable.
    • Load a .env file into a Node server.

Terminology

Development Dependency
Packages that are only needed for local development and testing.
Environment Variable
Configuration variables that are set to different values based on the environment you run your app in.

Materials

Key Takeaways

  • Install development dependencies with the --save-dev or -D flags:

      $ npm install nodemon --save-dev
    

    Or

      $ npm install nodemon -D
    
  • Development dependencies are installed when you run npm install. To prevent this you instead run npm install --production.
  • Installing a dependency globally is not recommended for production dependencies (you’re locked to one version of express, for example) but global dev dependencies are usually fine.
  • Permissions issues sometimes happen with global packages depending on the system.

2. express.Router()

Learning Objectives

  • Understand the purpose of the express.Router class.
  • Create router module with express.Router.
  • Export a route using modules.exports.
  • Load a route using app.use().

Materials

Key Takeaways

  • Router modules still need to load express with require().
  • Router methods (i.e. router.get()) behave largely the same as the parent express methods (i.e. app.get()).
  • Route modules append their endpoint paths to the path specified in the app.use() that loaded it. Together, these paths are joined to create the complete route path (i.e. /api/v0/definitions).

    In server.js:

      app.use('/api/v0', v0);
    

    In /routes/api/v0.js:

      router.get('/definitions', callback);
    

3. Assignment 5, Final project and group assignments


Open lab-time

  • Group hangs
    • Team names!
  • Mentor orientation

Dailies

  • Submit today’s Codepen/repo/gist to the Dailies section (in Assessments) in Brightspace.