koa2语法体验

//koa2:使用es678同步写法解决异步问题,相比于express解决了callback hell问题
const Koa = require("koa")    // npm 安装最新版koa
const app = new Koa()     // koa2创建server是通过new的方式

const asyncIO = ()=>{
return new Promise(resolve => setTimeout(resolve,500))
}
const mid = ()=> async (ctx,next)=>{
ctx.body = "mark"
await next()
ctx.body += "done"
}

app.use(mid())
app.use(async (ctx,next) => {
await asyncIO()
ctx.body += "saved"
await next()
})

app.listen(3000)

// 输出:mark saved done
posted @ 2018-10-04 00:34  前端大佬李嘉诚  阅读(176)  评论(0编辑  收藏  举报