CPNT 262 Day 15 - Deploying a server to Heroku

Housekeeping

  • Optional Lab Time this afternoon (no attendance)
  • Draft: Assignment 5 released after lunch

Trophy of the Day

  • Deployed Heroku website

1. Global npm packages and nodemon

Global npm packages are available for all npm projects and are usually used for packages that help you during development (i.e. those that aren't needed on the live server).

Materials

Key Takeaways

  • nodemon is usually the first package developers install so they don't have to stop and restart their servers.

    $ npm install -g nodemon
    

    Usage:

    $ nodemon server.js
    
  • 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. Try using sudo and enter your user password:

    $ sudo npm install -g nodemon
    

2. Route Handlers vs Middleware

Terminology

Route Handler
Events that fire only when a particular combination of HTTP Method AND Endpoint path are requested from a browser (i.e. app.get(), app.post(), etc).
Middleware
A generic event that fires for all HTTP Methods (i.e. app.use(function)). These often prepare the request and/or response for downstream middleware or routes but can also send the response directly.

Materials

Key Takeaways

  • Server requests are fulfilled on a first-come-first-served basis, so the order of your route handlers and middleware matter.
  • If a route sends a response, the connection is closed and downstream routes are not invoked.
  • Static file middleware is usually placed first in your app so that requests are fulfilled before dynamic responses are needlessly called (static files rarely need these resources).
  • 404 middleware is usually placed near the end of your app since successful calls would have already been fulfilled by then.

3. Testing a server with Postman

Materials


4. Deployment to Heroku

Materials


Lab Time

Activities

  1. Install nodemon globally.
  2. Deploy your static Express server from yesterday to Heroku.
  3. Install the Postman Health Check Collection and edit it to test your deployed Heroku URL.