官方文檔:koa
初識階段固然是看阮一峯的Koa 框架教程css
實戰教程(6)使用fetch有一部分講解html
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);