Express Fundamentals

Homework

  1. Review:
  2. Model-View-Controller
  3. Postman
  4. Express

Morning reflection

Housekeeping

  1. CPNT 201 Assignment 5 ZIPs
    • Feel free to not upload the entire winter-2021 repo.
    • Upload a README with links to your Issue and Pull Request.
  2. Announced after lunch:

1. Introduction to express

Learning Objectives

  • Summarize the MVC (Model-View-Controller) design pattern.
    • Understand how the directory structure of an express app relates to the MVC pattern.
    • Understand how middleware and routes relate to the MCV pattern.
  • Install the express package as a project dependency.
    • Create a “Hello World!” express app.
    • Start an express server with default port.
  • Review the CRUD design pattern.
    • Define route handler.
    • Understand the relationship between route methods and CRUD operations.
    • Fulfill a GET request using the app.get() route handler.

Terminology

Route
An event handler that fires for server requests that match a route method (i.e. GET, POST, etc) paired with a route path (i.e. /login, /api/definitions). These often respond with web files and/or json data. Routes are part of the Controller layer of the MVC pattern.

Materials

Key Takeaways


2. Routes and Middleware

Learning Objectives

  • Define middleware.
  • Understand the relationship between middleware and the request/response cycle of an express server.
  • Demonstrate how the order of route and middleware handlers affect the response sent from the server.
  • Refactor a “Hello World” app to serve static files.
  • Refactor a “Hello World” app to send 404 Not Found errors.

Terminology

Middleware
A generic event that fires for all server requests. These often prepare the request and/or response for downstream middlware or routes but can also send the response directly. Middlware handlers are part of the Controller layer of the MVC pattern.

Materials

Key Takeaways

  • Static file middleware is usually placed first in your app so that requests are filfilled 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. Heroku Deployment

Learning Objectives

  • Push a valid express project to Github.
  • Connect a GitHub repository to a Heroku app.
  • Deploy an express app to Heroku.

Materials

Key Takeaways

  • In order to function properly, Heroku needs your app to have the following:
    • a start script defined in your package.json, such as

        "scripts": {
          "start": "node server.js"
        }
      
    • A port number defined with process.env.PORT so that Heroku can override it with the standard port.

  • Sometimes (often?) Heroku will return an internal server error due to a misconfiguration. You can access the server logs from the admin panel menu in the top right:

Open lab-time

Activities

  1. Using a custom middleware handler, create a data logger that writes the current date and time to a log file for every server request.
  2. After today, you should have all you need to complete Assignment 3.

Dailies

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