Chapter 6. Connect

 

In this chapter

  • Setting up a Connect application
  • How Connect middleware works
  • Why middleware ordering matters
  • Mounting middleware and servers
  • Creating configurable middleware
  • Using error-handling middleware

Connect is a framework that uses modular components called middleware to implement web application logic in a reusable manner. In Connect, a middleware component is a function that intercepts the request and response objects provided by the HTTP server, executes logic, and then either ends the response or passes it to the next middleware component. Connect “connects” the middleware together using what’s called the dispatcher.

Connect allows you to write your own middleware but also includes several common components that can be used in your applications for request logging, static file serving, request body parsing, and session managing, among others. Connect serves as an abstraction layer for developers who want to build their own higher-level web frameworks, because Connect can be easily expanded and built upon. Figure 6.1 shows how a Connect application is composed of the dispatcher, as well as an arrangement of middleware.

Figure 6.1. The lifecycle of two HTTP requests making their way through a Connect server

6.1. Setting up a Connect application

6.2. How Connect middleware works

6.3. Why middleware ordering matters

6.4. Mounting middleware and servers

6.5. Creating configurable middleware

6.6. Using error-handling middleware

6.7. Summary