Home  >  Q&A  >  body text

node.js - koa-static出现generators will been removed in v3?

koa deprecated Support for generators will been removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/...,求解?

怪我咯怪我咯2676 days ago612

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 15:24:30

    找到方法了
    npm install koa-convert --save

    import convert from 'koa-convert'

    app.use(convert(require('koa-static')(__dirname + '/public')));

    reply
    0
  • PHPz

    PHPz2017-04-17 15:24:30

    看这里!

    是你仍然在用传统的 1.x 里的生成器语法才会出现这条提示,是 koa 善意的提醒啦,避免以后升到 v3 你的代码就瘫痪了!所以如果你仍然继续想使用生成器语法的话就借助 co 模块吧!

    官方的栗子:

    app.use(co.wrap(function *(ctx, next) {
      const start = new Date();
      yield next();
      const ms = new Date() - start;
      console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
    }));

    PS:既然你都用 v2 了,为啥不 async / await 用起来呢!

    reply
    0
  • Cancelreply