1. Middleware.prototype.use = function(fn){
    2. if(typeof fn !== 'function'){
    3. throw 'middleware must be a function';
    4. }
    5. this.cache.push(fn);
    6. return this;
    7. }
    8. Middleware.prototype.next = function(fn){
    9. if(this.middlewares && this.middlewares.length > 0 ){
    10. var ware = this.middlewares.shift();
    11. ware.call(this, this.next.bind(this));
    12. }
    13. }
    14. Middleware.prototype.handleRequest = function(){//执行请求
    15. this.middlewares = this.cache.map(function(fn){//复制
    16. return fn;
    17. });
    18. this.next();
    19. }