官方文档:
初识阶段当然是看阮一峰的前端用koa模拟动态接口
有一部分讲解
npm install koa koa-body koa-router --save-dev
var app = require('koa')();var router = require('koa-router')();var koaBody = require('koa-body')();router.get('/', function *(next) { this.body = 'hello koa !'});router.get('/api', function *(next) { this.body = 'test data'});router.get('/api/1', function *(next) { this.body = 'test data 1'});router.get('/api/2', function *(next) { this.body = { a: 1, b: '123' }});router.post('/api/post', koaBody, function *(next) { console.log(this.request.body) this.body = JSON.stringify(this.request.body)});app.use(router.routes()) .use(router.allowedMethods());app.listen(3000);