Skip to content

Middleware

Overview

When a server receives a HTTP request from the client (browser) it usually processes it and eventually returns some information to client via a HTTP response. This is known as the application’s request-response cycle. For a more in-depth understanding on request-response cycles, see The Request/Response Cycle of the Web. In Express, middleware are functions (or logic) that provide a convenient mechanism to handle HTTP requests entering your application. Middleware functions can be chained together to perform a series of logic, one after the other. The last middleware function will typically end the request-response cycle by returning a response to the client.

There are five(5) main categories of middleware that express supports, namely:

In this section you'll walk through an example of adding several middleware to Express.

Using Middleware in Express

First, you'll install a built-in middleware for parsing HTML form data, and then a third-party middleware that adds a layer of security to your application. Last, you'll see an illustration of how to create and add custom error-handling middleware.

  1. Add into src/app.js a built-in middleware in that parses incoming requests that contain JSON data:

    const express = require('express');
    const router = express.Router();
    
    const app = express();
    
    // Middleware
    app.use(express.json());
    
    ...
    
    module.exports = app;
    

    This allows Express to handle html form data that are sent to the server via a POST.

  2. Install Helmet:

    npm install helmet

  3. Import helmet in src/app.js:

    const express = require('express');
    const router = express.Router();
    const helmet = require('helmet');
    
    ...
    
    module.exports = app;
    

  4. Load helmet middleware:

    const express = require('express');
    const router = express.Router();
    
    const app = express();
    
    // Middleware
    app.use(express.json());
    app.use(helmet());
    
    ...
    
    module.exports = app;
    

    Helmet is a third-party middleware that adds some default protection to your Express application by setting various HTTP headers.

  5. Define a custom error-handling middleware function:

    const express = require('express');
    const router = express.Router();
    
    const app = express();
    
    // Middleware
    app.use(express.json());
    app.use(helmet());
    app.use(function (err, req, res, next) {
        //Extra logic for handling errors
        res.status(500).send('Something broke!');
    });
    ...
    
    module.exports = app;
    

    Error-handling middleware allows you to catch and process errors that occur within the application.

Congratulations 👏

That is for middleware! If it interests you, there is a lot more to middleware provided in the Express Middleware Documentation.

Conclusion

By the end of this section, you will have successfully learned the following:

  • That there are different types of middleware that Express supports.
  • How to add built-in, third-party, and error-handling middleware to Express applications.

Great job 🤗. Check out the next page if you've had any issues getting the project to work:

Troubleshooting