koa yield next; ReferenceError: next is not defined
发布于 8 年前 作者 zeroone001 6225 次浏览 来自 问答

var koa = require(‘koa’); var app = koa();

app.use(function *(){ var start = new Date(); yield next; var ms = new Date - start; this.set(‘X-Response-Time’,ms+‘ms’); });

app.use(function *(){ this.body = ‘hello world’; }); app.listen(3000); yield next;不是跟express中的next();类似吗,为什么会报错?

5 回复

官方是这样说的吧

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`);
}));

https://github.com/17koa/koa-generator 玩koa第一步,先建立两个目录 k1 k2 安装好相应的环境,再开始写代码

你的 next 形参掉了。。。

@i5ting 谢谢,我拉下next形参了

回到顶部