Middleware Flow Control

0.8.0 · abandoned · verified Wed Apr 22

middleware-flow is a JavaScript library providing enhanced control flow structures for Express-style middleware. It extends the basic sequential execution model by offering utilities for `series`, `parallel`, `parallelWait`, `each`, `or`, `and`, `if().then().else()`, `syncIf`, `asyncIf`, and `mwIf` operations. First published in 2015 and currently at version 0.8.0, it was developed primarily for CommonJS environments, common in older Node.js and Express applications, making it a useful tool for orchestrating complex middleware pipelines. Its key differentiator lies in its declarative API for non-sequential middleware execution, particularly for concurrent tasks or conditional branching, which are not natively provided by Express's core `app.use()` patterns. The project's development appears to be abandoned, with its last publish date being 11 years ago, and no further updates are expected.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates conditional middleware execution using `if().then().else()` and sequential execution with `series` in an Express application. The `/conditional` route varies its behavior based on a query parameter.

const express = require('express');
const { 'if': ifFlow, series } = require('middleware-flow');

const app = express();

// Dummy middlewares
const middlewareOne = (req, res, next) => {
  console.log('Middleware One executed.');
  next();
};
const middlewareTwo = (req, res, next) => {
  console.log('Middleware Two executed.');
  next();
};
const middlewareError = (req, res, next) => {
  console.error('Error Middleware executed.');
  res.status(500).send('An error occurred!');
};

// A synchronous function for ifFlow condition
function nameQueryExists (req, res) {
  return !!req.query.name;
}

app.use('/conditional', 
  ifFlow(nameQueryExists) // Checks if req.query.name exists
    .then(middlewareOne, middlewareTwo) // If true, run these sequentially
    .else(middlewareError) // If false, run the error middleware
);

app.use('/', series(middlewareOne, middlewareTwo, (req, res) => {
  res.send('Hello from middleware-flow! Try /conditional?name=test or /conditional');
}));

app.listen(3000, () => {
  console.log('Server listening on http://localhost:3000');
});

view raw JSON →