Using Compose Middleware
An introduction to Compose Middleware and a practical use case
What is Compose Middleware?
Compose Middleware is an NPM library that allows a user to combine multiple Express middleware functions into a single Express middleware handler. Compose Middleware also supports inline error handling. In other words, with Compose Middleware you can create a single middleware function that performs all of the functionality of several middleware functions, including error handling, rather than calling all of those functions separately.
But wait, what is Express Middleware?
To take a step back, Express lets us build websites using middleware functions. Any time we mount a function with app.use, the thing we mount is a middleware function (e.g., app.use(middleware) ). Every Express middleware function understands the same function signature: function(req, res, next). In other words, each middleware function has access to the request object, response object, and the next middleware function.
On additional requirement, is that if a middleware function does not end the request/response cycle, it must call `next`. You can imagine each middleware function as a link in a chain, with each link passing along the request and response objects to the next link.
How do I get started using Compose Middleware?
First, install Compose Middleware:
npm install compose-middleware –save
Then, create a new instance in your project. Note that you should have already installed and created an instance of Express.
var compose = require('compose-middleware').compose
Next, create your item of Compose Middleware. The compose function takes an array of Express middleware functions. Note that the functions in this array must follow the middleware function signature (i.e., req, res, next). The below example assumes that you will be creating and using your middleware function in different files.
module.exports = compose([ function (req, res, next) {}, function (err, req, res, next) {}, function (req, res, next) {}])
Now, in the file where you would like to use your newly defined middleware function, require this function and then insert the function in to your chain of other Express middleware functions.
const myMiddleware = require(./myfilepath). . .app.use(/myroute, myMiddleware())
When would I want to use Compose Middleware?
I came across Compose Middleware recently, while building an NPM library of my own that allows a user to visualize and test out SQL queries against the data in their current project. My goal was to architect the library so that applications using my library can mount a new route within a user’s project, and that route would host my query visualization page. To achieve this goal, I needed a mechanism to combine the necessary aspects of my project (including the API routes and html, bundle.js and other static front-end files) into a single express middleware function that could be published as an NPM library. Compose Middleware fit the bill exactly!
You can see my resulting library and code here and here.
And if you’d like to read more about publishing a middleware function created with the Compose Middleware library as an NPM library, hop over to this article written by my colleague Riley Butterfield, about the next step in our SQL visualization project.