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
- Reference: Downloading and installing packages globally
- Reference: nodemon
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 therequest
and/orresponse
for downstream middleware or routes but can also send theresponse
directly.
Materials
- Using Express Middleware in Express Docs
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
- Gist: Sample Postman Health Check Collection
- Stretch readings/watchings:
- Video: The Basics of Using Postman for API Testing by Steve Griffith
- Test script examples
- 10 Tips for Working with Postman Variables
4. Deployment to Heroku
Materials
Lab Time
Activities
- Install
nodemon
globally. - Deploy your static Express server from yesterday to Heroku.
- Install the Postman Health Check Collection and edit it to test your deployed Heroku URL.