meddleware
raw JSON → 3.0.4 verified Sat Apr 25 auth: no javascript
Configuration-based middleware registration for Express.js. Current stable version is 3.0.4. Meddleware allows you to define middleware in a configuration object with options like enabled, priority, route, and module. It supports loading middleware from npm modules or local files, specifying factory methods and arguments, and flow control (parallel, series). Key differentiators include priority-based ordering, route-based activation, and parallel/sequential execution control. The package has been around since Express 4.x and is part of the Kraken.js ecosystem. It is maintained with a focus on production usage.
Common errors
error TypeError: meddleware is not a function ↓
cause Importing the module incorrectly, e.g., using default import without esModuleInterop or named destructuring.
fix
Use
const meddleware = require('meddleware'); or enable esModuleInterop in TypeScript and use import meddleware from 'meddleware'; error Error: Cannot find module 'some-middleware' ↓
cause The module specified in config 'some-middleware' is not installed or not in the module search path.
fix
Install the module with
npm install some-middleware and ensure it is in the app's node_modules. For local modules, use a relative path like './lib/some-middleware'. error TypeError: middlewareFactory is not a function ↓
cause The module exported does not have a function matching the `method` name, or the module itself is not a function.
fix
Check the module's exports. Set
method to the exact exported function name, or omit it if the module itself is the middleware factory. Warnings
breaking In meddleware 2.0.0, the default value for the `enabled` option changed from `false` to `true`. Previously, omitting `enabled` would disable the middleware; now it enables it. ↓
fix Explicitly set `enabled: false` for middleware you want disabled, or upgrade to v3 which keeps the same behavior.
breaking In meddleware 3.0.1, registered middleware factories are now called with a context set to the method owner. Previously, the factory was called with a context of `null`. ↓
fix Check if your middleware factory relies on `this` being `null`; if so, update it to handle the new context.
deprecated The `priority` option is still used but may be deprecated in future versions in favor of explicit ordering. ↓
fix Consider using an ordered array of middleware definitions if you need strict ordering.
gotcha When configuring meddleware with JSON files, regex patterns in routes cannot be represented natively. You must use a shortstop handler (e.g., shortstop-regex) to convert strings to RegExp. ↓
fix Use a JSON preprocessor like shortstop with shortstop-regex, or define config in JavaScript instead of JSON.
gotcha String paths in `route` are automatically prefixed with any mountpath, but regular expressions are not. This can lead to unexpected behavior when using meddleware under a sub-app. ↓
fix If using regex routes, consider the mountpath manually or use string routes.
gotcha The `module.method` option defaults to the current middleware name, and then to the module itself. If your module exports a specific function, you may need to set `method` explicitly. ↓
fix Set `method` in the config to the exact exported function name you want to use.
gotcha Meddleware uses `require.resolve()` to find the module. If the module is not in the application's node_modules, it may fail. In v3.0.4, resolution was improved for modules outside the app's node_module. ↓
fix Upgrade to v3.0.4 or ensure all middleware modules are in the app's node_modules directory.
Install
npm install meddleware yarn add meddleware pnpm add meddleware Imports
- meddleware wrong
import meddleware from 'meddleware';correctconst meddleware = require('meddleware'); const express = require('express'); const app = express(); app.use(meddleware(config)); - default export wrong
const { meddleware } = require('meddleware');correctconst meddleware = require('meddleware'); app.use(meddleware(config)); - TypeScript type import wrong
const meddleware = require('meddleware');correctimport meddleware from 'meddleware'; app.use(meddleware(config));
Quickstart
const http = require('http');
const express = require('express');
const meddleware = require('meddleware');
const config = {
static: {
enabled: true,
priority: 20,
module: {
name: 'serve-static',
arguments: ['public']
}
},
security: {
enabled: true,
priority: 10,
module: {
name: './lib/security',
arguments: [{ maximum: true }]
}
}
};
const app = express();
app.use(meddleware(config));
http.createServer(app).listen(8080);