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

阅读 7.2k
2 个回答

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

import convert from 'koa-convert'

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

这里

是你仍然在用传统的 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 用起来呢!

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题