Node.js Middleware for Connect and Legacy Frameworks
raw JSON →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.
Common errors
error SyntaxError: Cannot use import statement outside a module ↓
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 ↓
"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. Warnings
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. ↓
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. ↓
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. ↓
Install
npm install middleware yarn add middleware pnpm add middleware Imports
- middleware wrong
import middleware from 'middleware';correctconst middleware = require('middleware'); - middleware wrong
import { middleware } from 'middleware';correctconst myMiddleware = require('middleware'); - middleware
const myMiddlewareFunction = require('middleware');
Quickstart
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.');
});