code: function handle(type, req, res, out) {
      var index = 0;
      var ended = false;
  
      // When called stop middlewares execution.
      res.end = end;
  
      // Handle next middleware in stack.
      function next(err) {
        var middleware = stack[index++];
  
        // No more middlewares or early end.
        if (!middleware || ended) {
          if (out) out(err, req, res);
          return;
        }
  
        // Check if middleware type matches or if it has no type.
        if (middleware.type.indexOf(type) === -1 && middleware.type.length > 0)
          return next(err);
  
        try {
          var arity = middleware.cb.length;
          //if err, only execute error middlewares
          if (err) {
            //error middlewares have an arity of 4, the first
            //arg being the error
            if (arity === 4) {
              middleware.cb(err, req, res, next);
            } else {
              next(err);
            }
          } else if (arity < 4) {
            middleware.cb(req, res, next);
          } else {
            next();
          }
        }
        catch (e) {
          next(e);
        }
      }
  
      // Stop middlewares execution.
      function end() {
        ended = true;
      }
  
      // Start handling.
      next();
    }
docs: Handle all middlewares of the provided
  type.
  
  @param {string} type the middleware type to
  handle
  @param {Object} req request object
  @param {Object} res response object
  @param {function} out optional function to
  be called once all middleware have been
  handled
  @returns the middlebot instance