搜尋

首頁  >  問答  >  主體

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/...,求解?

怪我咯怪我咯2873 天前753

全部回覆(2)我來回復

  • PHP中文网

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

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

    從 'koa-convert' 匯入轉換

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

    回覆
    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 用起來呢!

    回覆
    0
  • 取消回覆