i5ting / koa-generator

Koa' application generator for 1.x and 2.x( Express-style and support all middlewares include async/await )
https://github.com/17koa/koa-generator
MIT License
972 stars 110 forks source link

fix: route.allowedMethods registration failed #64

Closed NuoHui closed 3 years ago

NuoHui commented 3 years ago

app.use(index.routes(), index.allowedMethods())这样写法route.allowedMethods()应该没有注册到koa2 中间件里面去,我本地跑demo调试, 执行option也不会进去断点。 需要改成 app.use(index.routes()).use(index.allowedMethods()) 即可。没明白之前写法的作用是什么,请指教。

NuoHui commented 3 years ago

app.use() api

  /**
   * 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);
    return this;
  }