简单实现一个Koa类

话不多说,直接贴代码。

  • request.js
1
2
3
4
5
6
7
8
9
10
11
12
class Request {
constructor () {}
get url () {
return this.req.url;
}

get method() {
return this.req.method.toLowerCase()
}
}

module.exports = new Request();
  • response.js
1
2
3
4
5
6
7
8
9
10
11
class Response {
constructor () {}
get body () {
return this._body;
}
set body (val) {
this._body = val;
}
}

module.exports = new Response();
  • context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Context {
constructor () {}
get url () {
return this.request.url;
}
get body () {
return this.response.body;
}
set body (val) {
this.response.body = val;
}
get method () {
return this.request.method
}
}
  • koa.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const http = require('http');
const request = require('./request');
const response = require('./response');
const context = require('./context');

class Koa {
constructor () {
this.middlewares = [];
}
// 构建上下⽂
_createContext(req, res) {
const ctx = Object.create(context);
ctx.request = Object.create(request);
ctx.response = Object.create(response);
ctx.req = ctx.request.req = req;
ctx.res = ctx.response.res = res;
return ctx;
}
_compose (middlewares) {
return function(ctx) { // 传⼊上下⽂
return dispatch(0);
function dispatch(i) {
let fn = middlewares[i];
if (!fn) {
return Promise.resolve();
}
return Promise.resolve(
fn(ctx, function next() {// 将上下⽂传⼊中间件,mid(ctx,next)
return dispatch(i + 1);
})
);
}
}
}
use (middleware) {
this.middlewares.push(middleware);
}
listen (...args) {
const service = http.createServer(async (req, res) => {
let ctx = this._createContext(req, res)
// 中间件合成
const fn = this._compose(this.middlewares);
// 执⾏合成函数并传⼊上下⽂
await fn(ctx);

res.end(ctx.body)
})
service.listen(...args)
}
}

module.exports = Koa;
  • app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const Koa = require('./koa');
const app = new Koa()

app.use(async (ctx, next) => {
ctx.body = '1';
await next();
ctx.body += '5';
});
app.use(async (ctx, next) => {
ctx.body += '2';
await next();
ctx.body += '4';
});
app.use(async (ctx, next) => {
ctx.body += '3';
});

app.listen(8000, () => {
console.log('开始运行了')
})

// 12345