Stacked Middleware Bundler

1.1.1 · abandoned · verified Wed Apr 22

Stacked is a stand-alone, lightweight, and zero-dependency utility for bundling multiple middleware functions into a single, cohesive stack. Inspired by `connect`'s middleware infrastructure, it provides a `use` and `mount` API for composing HTTP middleware. As of its last published version, 1.1.1, the package has not seen updates since April 2017, indicating it is no longer actively maintained. Its key differentiator lies in its minimalist design, offering core middleware stacking functionality without the overhead or additional features found in more comprehensive frameworks. It is suitable for projects requiring a simple, independent middleware composition tool, particularly in environments compatible with its older CommonJS module format.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `stacked`, register multiple global middleware functions using `.use()`, and mount a specific middleware to a path using `.mount()`. It sets up a basic HTTP server to showcase how `stacked` handles incoming requests through the defined middleware chain, including path-specific logic.

const stacked = require('stacked');
const http = require('http');

/**
 * A simple logging middleware.
 */
function loggerMiddleware(req, res, next) {
  console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
  next();
}

/**
 * A middleware that adds a custom header.
 */
function headerMiddleware(req, res, next) {
  res.setHeader('X-Powered-By', 'Stacked');
  next();
}

/**
 * A specific middleware for a mounted path.
 */
function mountedPathMiddleware(req, res, next) {
  // req.url is stripped to the mount point here
  console.log('Mounted path URL:', req.url, 'Original URL:', req.originalUrl);
  res.end('Hello from mounted path!');
}

// Create the stacked middleware instance
const app = stacked()
  .use(loggerMiddleware)
  .use(headerMiddleware)
  .mount('/api', mountedPathMiddleware)
  .use((req, res, next) => {
    // This middleware only runs if the above .mount('/api') did not handle the request
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello from root!');
  });

// Create a Node.js HTTP server and use the stacked app as its request listener
const server = http.createServer(app);

const port = 3000;
server.listen(port, () => {
  console.log(`Server listening on http://localhost:${port}`);
  console.log('Try visiting:');
  console.log(`- http://localhost:${port}/`);
  console.log(`- http://localhost:${port}/api/data`);
});

view raw JSON →