Middleware Normalization Utility

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

Loadware is a utility library designed to simplify the process of aggregating and normalizing various middleware definitions into a unified array. It accepts middleware as strings (which are then `require()`d), functions, or arrays containing any combination of these types, making it flexible for applications that manage middleware from diverse sources. The current stable version is 2.0.0. However, the package was last published over nine years ago, indicating it is no longer actively maintained. This means there's no ongoing release cadence, and it lacks modern features or compatibility updates for newer Node.js versions or ECMAScript Modules (ESM). Its primary differentiator was its simple approach to consolidating disparate middleware formats at a time when such utilities were less common.

error Error: Cannot find module 'some-middleware-name'
cause You passed a string to `loadware` (e.g., `loadware('some-middleware-name')`), but the 'some-middleware-name' package is not installed in your project's `node_modules`.
fix
Install the missing package using npm install some-middleware-name or yarn add some-middleware-name.
error TypeError: loadware is not a function
cause This typically occurs in an ESM context where `require('loadware')` is used, or if `loadware`'s export structure is misunderstood, leading to incorrect access of the default function.
fix
Ensure you are using const loadware = require('loadware'); in a CommonJS file. If in an ESM file, you might need a CommonJS wrapper or a bundler to consume it, as direct import loadware from 'loadware'; is not supported.
breaking The `loadware` package is effectively abandoned, with its last publish date over nine years ago (January 2017). This means it is highly unlikely to receive security updates, bug fixes, or compatibility improvements for newer Node.js versions or ecosystem changes.
fix Consider migrating to actively maintained middleware composition libraries or implementing a custom solution for middleware normalization. For simple cases, direct `app.use()` calls with imported middleware might suffice.
gotcha Loadware exclusively uses CommonJS `require()` for resolving string-based middleware. It does not natively support ECMAScript Modules (ESM) or `import` statements.
fix Ensure your project is configured for CommonJS or use a transpilation step if integrating `loadware` into an ESM project. For string-based middleware, ensure the target package is CommonJS compatible and installed in `node_modules`.
gotcha When `loadware` receives a string as a middleware definition (e.g., 'body-parser'), it dynamically `require`s that module. If the specified package is not installed as a dependency in your project, `loadware` will fail to resolve it at runtime.
fix Always ensure that any middleware passed as a string to `loadware` is explicitly listed in your project's `package.json` and installed (e.g., `npm install body-parser`).
gotcha Due to its age, `loadware` may not be compatible with the latest versions of Node.js or related frameworks like Express.js, potentially leading to unexpected behavior or runtime errors.
fix Test `loadware` thoroughly with your specific Node.js and framework versions. If compatibility issues arise, consider alternative, more modern middleware management patterns or packages.
npm install loadware
yarn add loadware
pnpm add loadware

Demonstrates how to use `loadware` to combine string-based, function-based, and Express.js router middleware into a single array for an Express application.

const loadware = require('loadware');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();

let router = express.Router();
router.get('/', (req, res) => { res.send('Hello from router'); });

// loadware processes various middleware definitions
let middlewares = loadware(
  'cors', // Expects 'cors' package to be installed
  bodyParser.json(),
  (req, res, next) => {
    console.log('Custom inline middleware executed');
    next();
  },
  router // An Express.js router instance
);

// Apply the normalized middlewares to an Express app
app.use(middlewares);

app.get('/test', (req, res) => {
  res.json({ message: 'Test endpoint reached' });
});

const PORT = process.env.PORT ?? 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
  console.log('Try visiting / and /test');
});

// To make this runnable, install dependencies: npm install express body-parser cors