####概念:
Koa是基于nodejs平台的下一代web开发框架
node环境安装好的前提下,新建一个文件夹,进入该文件夹,使用npm init
使其支持npm
随后使用npm install koa --save
安装koa
新建server.js
,代码如下:
// Created by xiaoqiang on 02/06/2018.
const Koa = require('koa')
const app = new Koa()
const port = 3000
app.use(async(ctx, next) => {
ctx.body = 'hello koa'
})
app.listen(port)
console.log('listen on http://localhost:' + port)
使用node server.js
运行,如下:
打开localhost:3000
即可看到
代码解释:
将server.js
代码作一下修改,再运行
// Created by xiaoqiang on 02/06/2018.
const Koa = require('koa')
const app = new Koa()
const port = 3000
app.use(async(ctx, next) => {
ctx.body = '1'
next()
ctx.body += '2'
})
app.use(async(ctx, next) => {
ctx.body += '3'
next()
ctx.body += '4'
})
app.use(async(ctx, next) => {
ctx.body += '5'
next()
ctx.body += '6'
})
app.use(async(ctx, next) => {
ctx.body += '7'
next()
ctx.body += '8'
})
app.listen(port)
console.log('listen on http://localhost:' + port)
运行结果为:
解释:代码中执行完ctx.body = '1'
后,执行了ctx.next()
,即跳过了ctx.body += '2'
执行下一个中间件,接下来的以此类推;执行到最后一个中间后,开始往回跑,即先执行ctx.body += 8
,可以得到结果:13578642
即:
修改 server.js
,添加delay()方法
,源代码如下:
// Created by xiaoqiang on 02/06/2018.
const Koa = require('koa')
const app = new Koa()
const port = 3000
function delay () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, 1000)
})
}
app.use(async(ctx, next) => {
ctx.body = '1'
await next()
ctx.body += '2'
})
app.use(async(ctx, next) => {
ctx.body += '3'
await delay()
await next()
ctx.body += '4'
})
app.use(async(ctx, next) => {
ctx.body += '5'
await next()
ctx.body += '6'
})
app.use(async(ctx, next) => {
ctx.body += '7'
next()
ctx.body += '8'
})
app.listen(port)
console.log('listen on http://localhost:' + port)
node server.js
打开localhost:3000
,一秒后可见到13578642
,之所以加了延迟函数后,顺序没有改变,正式因为使用了 Async+await异步处理机制。
接下来我们利用Async+await异步处理机制,自己实现koa-logger中间件,打印所有请求的耗时。
新建koa-logger.js
,源码如下:
// Created by xiaoqiang on 02/06/2018.
module.exports = async(ctx, next) => {
// 记录所有请求开始时间
const start = new Date().getTime()
// 等待所有请求结束
await next()
// 记录所有请求结束时间
const end = new Date().getTime()
// 打印请求及时间信息
console.log('请求地址:' + ctx.request.url , '耗时' + (end - start), '响应内容长度' + ctx.body.length)
}
在server.js
中引用
// Created by xiaoqiang on 02/06/2018.
const Koa = require('koa')
const app = new Koa()
const port = 3000
const KoaLogger = require('./koa-logger')
function delay () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, 1000)
})
}
app.use(KoaLogger)
app.use(async(ctx, next) => {
ctx.body = '1'
await next()
ctx.body += '2'
})
app.use(async(ctx, next) => {
ctx.body += '3'
await delay()
await next()
ctx.body += '4'
})
app.use(async(ctx, next) => {
ctx.body += '5'
await next()
ctx.body += '6'
})
app.use(async(ctx, next) => {
ctx.body += '7'
next()
ctx.body += '8'
})
app.listen(port)
console.log('listen on http://localhost:' + port)
运行server.js
,打开localhost:3000
,
可见控制台输出(请求改为响应,之前写错了):
解释:
ctx.request.url
为/
使用cnpm install koa-router --save
安装koa-router
, 修改server.js
,源代码为:
// Created by xiaoqiang on 02/06/2018.
const Koa = require('koa')
const app = new Koa()
const port = 3000
const KoaLogger = require('./koa-logger')
const Router = require('koa-router')
const router = new Router()
app
.use(router.routes())
.use(router.allowedMethods())
router.get('/', (ctx, next) => {
ctx.body = 'index'
})
router.get('/login', (ctx, next) => {
ctx.body = 'login'
})
router.get('/register', (ctx, next) => {
ctx.body = 'register'
})
app.use(KoaLogger)
app.listen(port)
console.log('listen on http://localhost:' + port)
node server.js
运行,打开localhost:3000
,可见:
控制台输出:
输入localhost:3000/login
,可见:
控制台输出: