Node.js Middleware for Connect and Legacy Frameworks

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

This `middleware` package, authored by Tim Smart, provides a fundamental utility for composing functions within a request-response cycle. It was specifically designed for integration with older Node.js web frameworks like Connect and Biggie-Router, which were prevalent around its initial publication date. The package, last updated to version 1.0.0 approximately 13 years ago (around 2013), represents an early and minimalist implementation of the middleware pattern that later became a cornerstone of more widely adopted frameworks such as Express.js. It features zero direct dependencies, offering a lean and unopinionated approach to chaining processing steps in a server's request-handling pipeline. Due to its significant age, lack of a `README.md` file on npm, and the complete absence of any recent updates or active development, this package is considered abandoned. It is not recommended for new Node.js projects, which should instead utilize modern, actively maintained middleware solutions. Its utility is now largely confined to understanding historical Node.js patterns or for maintaining highly specific legacy applications built within its original ecosystem.

error SyntaxError: Cannot use import statement outside a module
cause Attempting to use `import middleware from 'middleware';` in a CommonJS-only Node.js environment or a modern ESM environment without proper interoperability setup.
fix
For this legacy package, you must use const middleware = require('middleware');. If your project is ESM, consider migrating to modern alternatives or carefully setting up CommonJS compatibility.
error TypeError: require is not a function
cause This error occurs if you try to use `require()` within an ES Module (ESM) file without explicitly setting up CommonJS compatibility, which is the default in ESM.
fix
If your file is an ES Module (e.g., "type": "module" in package.json or .mjs extension), require is not available by default. Use const { createRequire } = require('module'); const require = createRequire(import.meta.url); const middleware = require('middleware'); as a workaround, but it's strongly advised to migrate to modern, ESM-compatible solutions instead of using this abandoned package.
breaking This package is effectively abandoned. It was last published 13 years ago (around 2013) to version 1.0.0. There is no active maintenance, bug fixes, or security patches, making it unsuitable for new projects.
fix Avoid using this package for new development. Migrate legacy applications to modern, actively maintained middleware solutions (e.g., Express.js, Koa, or custom middleware functions) that are compatible with current Node.js versions and best practices.
gotcha The package was developed before native ES Modules (ESM) were standard in Node.js. It uses CommonJS (`require`) exclusively. Attempting to `import` this package in an ESM context will result in module resolution errors.
fix Ensure your project is configured for CommonJS, or use dynamic `import()` if absolutely necessary within an ESM context, though this is not recommended for an abandoned package. Prefer modern alternatives with native ESM support.
deprecated This package was designed for a legacy Node.js ecosystem, primarily in conjunction with frameworks like Connect and Biggie-Router. Its API and internal workings may not be compatible with modern Node.js versions (e.g., v14, v16, v18+) or contemporary web frameworks.
fix Modern Node.js development should utilize actively maintained middleware patterns and frameworks. If maintaining a legacy application, thoroughly test compatibility with specific Node.js versions.
npm install middleware
yarn add middleware
pnpm add middleware

This quickstart demonstrates how to conceptually use the `middleware` package within a simple Node.js HTTP server. It shows the CommonJS `require` syntax and a basic `(req, res, next)` signature, simulating how such a middleware might be integrated, typically with a framework like Connect.

const http = require('http');
const middleware = require('middleware'); // Assuming 'middleware' exports a function (req, res, next)

// A simulated final handler function for the HTTP server
const finalHandler = (req, res) => {
  if (!res.headersSent) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end(`Request URL: ${req.url}, Processed by middleware.\n`);
  }
};

// Create a basic HTTP server
const server = http.createServer((req, res) => {
  // In a real Connect/Express app, the framework handles the 'next' orchestration.
  // Here, we manually simulate a simple middleware chain.
  middleware(req, res, (err) => {
    if (err) {
      console.error('Middleware encountered an error:', err);
      if (!res.headersSent) {
        res.writeHead(500, { 'Content-Type': 'text/plain' });
        res.end('Internal Server Error\n');
      }
      return;
    }
    // If middleware calls next, proceed to the final handler
    finalHandler(req, res);
  });
});

server.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
  console.log('Access http://localhost:3000 in your browser.');
});