Compose Connect Middleware

raw JSON →
0.0.1 verified Thu Apr 23 auth: no javascript abandoned

connect-compose is a minimalist JavaScript utility designed to combine multiple `connect` middleware functions into a single, cohesive middleware. Published at version 0.0.1 nearly a decade ago, this package has seen no further development and is effectively abandoned. The underlying `connect` framework, while still functional, is also largely considered superseded by more comprehensive web frameworks like Express.js, which itself originated from `connect`. Developers seeking similar middleware composition functionality for modern Node.js applications, especially those using ESM, should look for actively maintained alternatives as `connect-compose` is not compatible with contemporary development practices and ecosystems.

breaking This package is very old (last published ~10 years ago) and targets the connect@1.x/2.x era. It is not designed for modern Node.js versions or ES Modules.
fix For new projects, consider alternatives like 'compose-middleware' or building on modern frameworks like Express.js directly, which includes similar functionality intrinsically. Ensure your project uses CommonJS and an older Node.js version if you must use this package.
gotcha connect-compose is CommonJS-only. Attempting to use `import` statements for this package in an ES Module context will result in a runtime error.
fix Use `const compose = require('connect-compose');` for importing. If your project is ESM-first, you will need to wrap this in a CommonJS compatibility layer or use a different, ESM-compatible middleware composer.
deprecated The underlying `connect` framework itself has not been actively developed since 2019 (version 3.7.0). Many applications have migrated to `Express.js` or other modern HTTP frameworks.
fix Evaluate migrating to Express.js or a similar framework that offers built-in middleware handling and active community support. If using `connect`, be aware of the lack of recent updates and potential security concerns.
npm install connect-compose
yarn add connect-compose
pnpm add connect-compose

Demonstrates composing two simple middleware functions (`mw1`, `mw2`) into one using `connect-compose`, then applying it to a `connect` application alongside a final response-sending middleware.

const connect = require('connect');
const compose = require('connect-compose');
const http = require('http');

const app = connect();

// Middleware 1: Adds a property to the request object
function mw1 (req, res, next) {
  req.stuff = ['mw1'];
  next();
}

// Middleware 2: Modifies the property added by mw1
function mw2 (req, res, next) {
  req.stuff.push('mw2');
  next();
}

// Final middleware: Sends the aggregated 'stuff' as a response
function mw3 (req, res) {
  res.end(req.stuff.join(', '));
}

// Compose mw1 and mw2 into a single middleware and apply it
app.use(compose([mw1, mw2]));

// Apply the final middleware
app.use(mw3);

// Create an HTTP server and listen on port 6060
const server = http.createServer(app);
server.listen(6060, () => {
  console.log('Connect app listening on http://localhost:6060');
  console.log('Try visiting http://localhost:6060 in your browser.');
});