Composable Middleware

0.3.1 · abandoned · verified Wed Apr 22

Composable Middleware allows developers to treat a sequence of Connect, Flatiron/Union, or hybrid middleware functions as a single, unified middleware function. The package, currently at version 0.3.1, is explicitly marked as abandoned and no longer actively maintained. Its core differentiation lies in its minimal overhead design, focusing solely on function composition without built-in support for URL routing, path mounting, or comprehensive error handling (like 404/500 responses), which are left to the parent framework. It identifies middleware types based on function arity (e.g., `(req,res,next)` for Connect normal middleware, `(err,req,res,next)` for error handling). Given its abandoned status, there is no active release cadence, and users are encouraged to consider maintained forks or alternative solutions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to compose multiple middleware functions into a single unit using `composable-middleware` and integrate it into a `connect` application. It includes basic logging and a response.

const connect = require('connect');
const composable_middleware = require('composable-middleware');

// Mock connect middleware for demonstration
const logger = (req, res, next) => {
  console.log(`${req.method} ${req.url}`);
  next();
};

const greet = (req, res, next) => {
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello from composed middleware!\n');
  next(); // Even though we end, pass to next for completion or logging
};

const finalHandler = (req, res) => {
  if (!res.headersSent) {
    res.statusCode = 404;
    res.end('Not Found\n');
  }
};

// Compose middleware steps
const composedMw = composable_middleware(logger, greet);

// Create a Connect app and use the composed middleware
const app = connect();
app.use(composedMw);
app.use(finalHandler);

const PORT = process.env.PORT ?? 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
  console.log('Try accessing http://localhost:3000/');
});

view raw JSON →