Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including dynamic-middleware with all npm packages installed. Try it out:

var dynamicMiddleware = require("dynamic-middleware")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

dynamic-middleware v4.0.2

turn a connect middleware into a replaceable, deletable middleware

dynamic-middleware Build Status

Replace or disable a connect/express middleware in runtime

compatible with express 3, express 4 and connect 3


its quite inconvenient to replace a middleware after you start a connect / express server


    npm install dynamic-middleware


const DynamicMiddleware = require('dynamic-middleware')
const express = require('express')

const app = express()

// a simple middleware
function myMiddleware(req, res, next) {

// create a dynamic one from it
let dm = DynamicMiddleware.create(myMiddleware)

app.get('/', dm.handler()) 

// disable the middleware, will reply with 404 now

// enable it back

// or replace it with something else
dm = dm.replace(function(req, res, next) {

// create a dynamic error middlware
let errorDm = DynamicMiddleware.create((err, req, res, next) => { ... })


see also

loadbalance module

loadbalance-middleware module



Previous versions operated by manipulating the internal state of express/connect , this approach worked well for real middlewares (.use()) but was terrible for routes (.get() etc..). The new version does not do that, but rather manages the state internally.


The public interface of this module has changed


MIT © yaniv kessler

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free