Express Fundamentals
Homework
- Review:
- Model-View-Controller
- Read: MVC definition on MDN
- Read: Model-view-controller on Wikipedia
- Postman
- Watch: The Basics of Using Postman for API Testing by Steve Griffith
- Express
- Read: Express/Node introduction
- Read: Setting up a Node development environment on MDN
- Read: Serving static files in Express in Express Docs
- Read: Using Express Middleware in Express Docs
- Watch: The first 20 minutes of How to build a REST API with Node js & Express by Mosh Hamedani (we’ll eventually cover most of this video)
Morning reflection
Housekeeping
- CPNT 201 Assignment 5 ZIPs
- Feel free to not upload the entire
winter-2021repo. - Upload a README with links to your Issue and Pull Request.
- Feel free to not upload the entire
- Announced after lunch:
- Assignment 4: JSON gallery endpoint with
fetch - DRAFT: Final Project
- Assignment 4: JSON gallery endpoint with
1. Introduction to express
Learning Objectives
- Summarize the MVC (Model-View-Controller) design pattern.
- Understand how the directory structure of an
expressapp relates to the MVC pattern. - Understand how middleware and routes relate to the MCV pattern.
- Understand how the directory structure of an
- Install the
expresspackage as a project dependency.- Create a “Hello World!”
expressapp. - Start an
expressserver with default port.
- Create a “Hello World!”
- Review the CRUD design pattern.
- Define route handler.
- Understand the relationship between route methods and CRUD operations.
- Fulfill a
GETrequest using theapp.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/orjsondata. Routes are part of the Controller layer of the MVC pattern.
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.
2. Routes and Middleware
Learning Objectives
- Define middleware.
- Understand the relationship between middleware and the request/response cycle of an
expressserver. - Demonstrate how the order of route and middleware handlers affect the
responsesent from the server. - Refactor a “Hello World” app to serve static files.
- Refactor a “Hello World” app to send
404 Not Founderrors.
Terminology
- Middleware
- A generic event that fires for all server requests. These often prepare the
requestand/orresponsefor downstream middlware or routes but can also send theresponsedirectly. 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).
404middleware 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
expressproject 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.PORTso 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:
- “More” -> “View logs”
- You can also install the Heroku CLI (command line interface) to pull the logs from your terminal.
Open lab-time
Activities
- Using a custom middleware handler, create a data logger that writes the current date and time to a log file for every server request.
- 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.
