1 thought on “One-time middleware?

  1. For now, unless I hear back from you, I’ll leave this example for a single one:

    app.use(oneTime(middleware))
    
    function oneTime(fn) {
      var done = false
      return function (req, res, next) {
        if (done) {
          next()
          return
        }
    
        fn(req, res, next)
      }
    }

    And for a collection:

    app.use(multipleOneTime(mw1, mw2, mw3))
    
    function multipleOneTime(arr) {
      var router = express.Router()
    
      router.use.apply(router, arguments)
    
      return oneTime(router)
    }
    
    function oneTime(fn) {
      var done = false
      return function (req, res, next) {
        if (done) {
          next()
          return
        }
    
        fn(req, res, next)
      }
    }

Comments are closed.