nodejs: middleware should remove itself from the stack

I have a middleware that runs a sequence of actions once and then I don't wish to use it anymore when new requests arrive. Is there a way for my middleware to remove itself from the stack?

Thanks, Li

Yes, there is. Consider this:

var app = require('express')();
function myHandler(req, res, next) {
  //do something usefull

  //locate this handler
  var handlerIndex = -1;
  for(var i =0; i < app.stack.length; i++) {
    if (app.stack[i].handle === myHandler) {
      handlerIndex = i;
    }
  }
 if (handlerIndex > -1) {
    app.stack.splice(handlerIndex, 1);
 }
 next();
});

app.use("/api", myHandler);

Treat this as symbolic code, I have not chance to test it but the concept is there...