app.use源码


  const isGeneratorFunction = require('is-generator-function');
  const debug = require('debug')('koa:application');
 /**
   * Use the given middleware `fn`.
   *
   * Old-style middleware will be converted.
   *
   * @param {Function} fn
   * @return {Application} self
   * @api public
   */

  use(fn) {
    if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');
    if (isGeneratorFunction(fn)) { //代码转换用的
      deprecate('Support for generators will be removed in v3. ' +
                'See the documentation for examples of how to convert old middleware ' +
                'https://github.com/koajs/koa/blob/master/docs/migration.md');
      fn = convert(fn);
    }
    debug('use %s', fn._name || fn.name || '-');
    this.middleware.push(fn);  //最开始初始化会创建中间件空数组 ,其实use就是放到middleware数组中
    return this;
  }

原文地址:https://www.cnblogs.com/TTblog5/p/13097259.html