Module 3 - Express Middleware

Objectives

Types of Middleware

There are different types of middleware; for our purposes, we'll group them into three different categories:

  1. Built-in middleware
  2. Third-party middleware
  3. Custom middleware

Built-in Middleware

Built-in middleware is included with Express but not added to the application automatically. Like the other types, we need to opt-in to using it in our application.

We saw an example of built-in middleware when we added support for parsing JSON content out of the request body using server.use(express.json());.

Every type of middleware works in the same way. We tell Express about the middleware we want to turn on for our application by making a call to .use() on our server and passing .use() the piece of middleware we want to apply. This line must come after the server has been created by calling express().

Third-party Middleware

Third-party middleware are npm modules that we can install and then import into our applications using require(). There are thousands of middleware modules we can use. There is no need to write our own in most cases.

Some popular middleware modules are:

Custom Middleware

Custom middleware is functions we write to perform specific tasks. We'll learn more about how to write and use them in the next section.

Challenge

Write a paragraph (or two) explaining Express middleware and how you would use it when writing Web APIs.

Guided Project

Project Resources

Important Notes

The versions of project dependencies used in the recording are slightly different from the ones used in the starter and solution repositories, but this should not affect the relevant code of the Guided Project.

The versions used in the repositories are more recent, and thus more similar to the versions you will install if you create a project from scratch.

Assignment

Project Resources