Dynamic Middleware
raw JSON →dynamic-middleware is a utility library for Connect and Express applications that enables runtime replacement or disabling of middleware. It addresses the common difficulty of altering middleware after an HTTP server has started, a feature not natively supported by Connect or Express. The current stable version is 4.0.4. The library's release cadence is not specified but it has undergone significant architectural changes between major versions. It differentiates itself by providing a `create` method to wrap existing middleware, allowing for dynamic control over its execution via `enable()`, `disable()`, and `replace()` methods, specifically for `app.use()` and `app.get()` style middleware, which was improved in `4.x.x` to handle routes more effectively.
Common errors
error TypeError: dynamic_middleware_1.default is not a function ↓
const DynamicMiddleware = require('dynamic-middleware') to import the module correctly. error TypeError: Cannot read properties of undefined (reading 'create') ↓
const DynamicMiddleware = require('dynamic-middleware') and that DynamicMiddleware is not null or undefined before calling .create(). error Middleware not affecting routes (e.g., app.get('/')) as expected, only app.use() ↓
dynamic-middleware version 4.x.x or later, which has an improved internal state management system specifically designed to work correctly with route-specific middleware. Remember to update your code to the new API if upgrading from a 3.x.x version. error TypeError: dm.handler is not a function ↓
.handler() on the DynamicMiddleware instance when integrating it into the Express/Connect application: app.get('/', dm.handler()). For error middleware, use .errorHandler(). Warnings
breaking From `3.x.x` to `4.x.x`, the internal mechanism for managing middleware changed significantly. Older versions directly manipulated Express/Connect's internal state, which caused issues with route-specific middleware (`.get()`, `.post()`). Version `4.x.x` manages state internally, providing more robust handling for all middleware types. ↓
breaking The public interface of `dynamic-middleware` changed completely in `4.x.x`. Code written for `3.x.x` and earlier will require updates to adapt to the new API. ↓
gotcha The provided examples and package structure strongly suggest `dynamic-middleware` is primarily a CommonJS module. Using ES module `import` syntax might lead to compatibility issues or require specific Node.js configuration for CJS interop, especially for older Node versions or without a transpilation step. ↓
gotcha The library explicitly states compatibility with `express 3`, `express 4`, and `connect 3`. While it might work with newer versions of Express (e.g., Express 5), official compatibility is not guaranteed beyond these stated versions. Newer Express versions might introduce breaking changes that affect `dynamic-middleware`'s internal mechanisms. ↓
Install
npm install dynamic-middleware yarn add dynamic-middleware pnpm add dynamic-middleware Imports
- DynamicMiddleware wrong
import DynamicMiddleware from 'dynamic-middleware'correctconst DynamicMiddleware = require('dynamic-middleware') - dm.handler() wrong
app.get('/', dm)correctapp.get('/', dm.handler()) - dm.errorHandler() wrong
app.use(errorDm)correctapp.use(errorDm.errorHandler())
Quickstart
const DynamicMiddleware = require('dynamic-middleware')
const express = require('express')
const app = express()
// a simple middleware
function myMiddleware(req, res, next) {
res.end('1')
}
// create a dynamic one from it
let dm = DynamicMiddleware.create(myMiddleware)
app.get('/', dm.handler())
// disable the middleware, will reply with 404 now
// dm.disable()
// enable it back
// dm.enable()
// or replace it with something else
dm = dm.replace(function(req, res, next) {
res.end('2')
})
// create a dynamic error middlware
let errorDm = DynamicMiddleware.create((err, req, res, next) => {
console.error(err);
res.status(500).send('Error: ' + err.message);
})
app.use(errorDm.errorHandler())
app.listen(3000, () => {
console.log('Server running on port 3000');
});