node

koa 学习笔记

一,初始化koa 1, npm init -y //初始化项目 2,npm install -g cnpm –registry=https://registry.npm.taobao.org //切换镜像 3, cnpm install koa –save //安装koa 4, 编写基本代码 const koa=require(‘koa’); const app = new koa(); app.use(async(cxt)=>{ cxt.body=‘我运行成功了’ }) app.listen(3000,()=>{ console.log(‘我在3000端口运行’) }) 5,跑成功了 二, 准备es6语法,我这里就不写了 三,在写一遍scync 和 awite 和 Promise function testAwait(){ console.log(“testAwait”); } async function helloAsync(){ await testAwait(); //等待上面函数解决结果 console.log(“helloAsync”); } 四,路由 1, cnpm install koa-router –save 安装路由 2,路由使用 const Koa = require('koa'); const Router = require('koa-router');//引入路由 const app = new Koa(); const router = new Router(); router.