code: function next(err, cb) {
      if (typeof err === 'function') {
        debug('pushed filter callback');
        cb = err;
        err = null;
      }
      if (typeof cb === "function") callbacks.push(cb);
  
      debug('call middleware index', index);
      var layer = stack[index++];
  
      // No more middlewares or early end.
      if (!layer || ended) {
        debug('middleware-s call done');
        fn = callbacks.pop();
        while (fn) {
          err = fn(err, ctx);
          if (err) break;
          fn = callbacks.pop();
        }
        if (out) return out(err, ctx);
        if (err) throw err;
        return;
      }
  
      // Check if middleware type matches or if it has no type.
      if (layer.type.length > 0 && layer.type.indexOf(type) < 0) {
        return next(err);
      }
  
      try {
        var arity = layer.handle.length;
        //if err, only execute error middlewares
        if (err) {
          //error middlewares have an arity of 3, the first
          //arg being the error
          if (arity === 3) {
            layer.handle(err, ctx, next);
          } else {
            next(err);
          }
        } else if (arity < 2) {
          layer.handle(ctx);
          if (!ended) next();
        } else if (arity < 3) {
          layer.handle(ctx, next);
        } else {
          next();
        }
      }
      catch (e) {
        next(e);
      }
    }
docs: Handle next middleware in stack.