-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy-koa.js
70 lines (65 loc) · 1.88 KB
/
my-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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const http = require('http');
class MyServer {
static privateListKey = Symbol("list");
static privateServerKey = Symbol("server");
constructor() {
const handlers = this[MyServer.privateListKey] = [];
this[MyServer.privateServerKey] = http.createServer((req, res) => {
/** 1.运行时绑定参数 */
function wraperHandler(handler) {
return async () => {
if (!handler) return;
let index = handlers.findIndex(item => item == handler);
const nextHandler = handlers[index + 1];
await handler(req, res, wraperHandler(nextHandler));
}
}
wraperHandler(handlers[0])(req, res);
});
}
use(handler) {
this[MyServer.privateListKey].push(handler);
}
listen(port, path, callback) {
/** 2. 执行之前倒着绑定, 利用柯里化提前在所有函数执行前绑定好参数 */
let nexts = this[MyServer.privateListKey];
let prev = async (next) => { await next() };
let next;
for (let i = nexts.length - 1; i >= 0; i--) {
next = nexts[i];
prev = next.bind(this, prev);
}
//next(req,res); <- createServer
this[MyServer.privateServerKey].listen(port, path, callback);
}
}
const app = new MyServer();
app.use(async (req, res, next) => {
console.log("1 start");
await next();
console.log("1 end");
})
app.use(async (req, res, next) => {
console.log("2 start");
await next();
await new Promise((resolve) => {
setTimeout(() => {
console.log("timeout 2秒 结束");
resolve();
}, 2000);
});
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('okay');
console.log("2 end");
})
app.use(async (req, res, next) => {
console.log("3 start");
await next();
console.log("3 end");
})
// Now that server is running
app.listen(1337, '127.0.0.1', () => {
http.get('http://127.0.0.1:1337/', (res) => {
console.log("request end")
});
});