Composable Middleware
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
-
TypeError: composable_middleware is not a function
cause Attempting to use `composable_middleware` as a function when it was imported incorrectly, likely as a named export in an ESM context, or without a `require` call.fixEnsure you are using `const composable_middleware = require('composable-middleware');` for CommonJS projects or if dynamically importing in ESM. -
ERR_REQUIRE_ESM
cause You are attempting to `require()` an ESM module, or more likely, you are in an ESM context and attempting to `import` this CJS-only package directly without interop.fixIf in an ESM module, you must use dynamic import `const composable_middleware = await import('composable-middleware');` or refactor your project to use CommonJS where this package is needed. The `composable-middleware` package itself is CommonJS. -
Cannot read properties of undefined (reading 'use')
cause This error often occurs if `composable_middleware` itself is `undefined` because `require('composable-middleware')` failed, or if you tried `composable_middleware.use()` directly without first invoking `composable_middleware()` to get an instance.fixVerify the package is installed and `require()` is correct. Ensure you invoke the main export to get an instance before calling `.use()`: `const mw = composable_middleware().use(someMiddleware);`
Warnings
- breaking This package is explicitly marked as 'Abandoned' by its maintainers. It is no longer actively developed or supported, which means no new features, bug fixes, or security patches will be released. Using it in production carries significant risk.
- gotcha The package is CommonJS-only and does not support native ES Modules (ESM) `import` statements directly. Attempting to use `import` without a CJS interop layer will result in errors.
- gotcha Composable Middleware intentionally does not handle routing, path mounting, or default error responses (like 404 Not Found or 500 Internal Server Error). Developers must provide their own 'finalware' or rely on the overarching framework for these concerns.
- gotcha The package targets Node.js versions `>=0.8.0`. While it might run on newer Node.js versions, it has not been tested or updated for modern Node.js APIs or performance optimizations. Compatibility issues or subtle bugs may arise.
Install
-
npm install composable-middleware -
yarn add composable-middleware -
pnpm add composable-middleware
Imports
- composable_middleware
import composable_middleware from 'composable-middleware';
const composable_middleware = require('composable-middleware'); - composable_middleware().use
import { use } from 'composable-middleware';const mw = require('composable-middleware')().use(logger);
Quickstart
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/');
});