CNode

koa yield next; ReferenceError: next is not defined

问答
Zzeroone001发布于10 年前最后回复10 年前5 回复7700 浏览0 收藏

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)

Z
zeroone001#1·10 年前

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

Z
zeroone001#2·10 年前

@yakczh @magicdawn 谢谢~~

I
i5ting#3·10 年前

官方是这样说的吧

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`);
}));
Y
yakczh#4·10 年前

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

M
magicdawn#5·10 年前

你的 next 形参掉了。。。

参与回复
登录后即可参与回复。登录