koa2第一天

router.get("/hello",async(ctx )=>{
const a=await new Promise(reslove=>reslove(123))

const b=await Promise.resolve(134)

ctx.body={
a,b
}
})


参数ctx是由koa传入的封装了request和response的变量,我们可以通过它访问request和response,next是koa传入的将要处理的下一个异步函数。

ctx.body{}
等价于ctx.reponse.body{}

 浏览器显示

{
  "a": 123,
  "b": 134
}

猜你喜欢

转载自www.cnblogs.com/chargeworld/p/10031516.html