Proper way to remove middleware from the Express stack?

use actually comes from Connect (not Express), and all it really does is push the middleware function onto the app's stack.

So you should be just fine splicing the function out of the array.

However, keep in mind there is no documentation around app.stack nor is there a function to remove middleware. You run the risk of a future version of Connect making changes incompatible with your code.


This is a useful functionality if you are inheriting some unwanted middleware from a framework built on express.

Building on some of the answers that came before me: In express 4.x the middleware can be found in app._router.stack. Note that the middleware are invoked in order.

// app is your express service

console.log(app._router.stack)
// [Layer, Layer, Layer, ...]

Tip: You can search the individual layers for the one you want to remove/move

const middlewareIndex = app._router.stack.findIndex(layer => {
 // logic to id the specific middleware
});

Then you can just move/remove them with standard array methods like splice/unshift/etc

// Remove the matched middleware
app._router.stack.splice(middlewareIndex, 1);