-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.js
More file actions
58 lines (49 loc) · 1.74 KB
/
Application.js
File metadata and controls
58 lines (49 loc) · 1.74 KB
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
const http = require('http') // 利用 http 模块
const Context = require('./Context') // 导入 Context 模块
module.exports = class Application {
constructor() {
this.middlewares = [] // 保存所有的中间件函数
}
// 构建ctx,传入到中间件集合,执行next递归
callback() {
return async(req, res) => {
// 初始化ctx
const ctx = new Context(req, res);
//调用 compose 函数,依次处理所有中间件函数
await this.compose(this.middlewares)(ctx)
// 最后返回res body
this.responseBody(ctx)
}
}
// 简单粗暴处理res body
responseBody(ctx) {
const content = ctx.body;
ctx.res.end(content);
console.log(content)
}
// 核心:递归中间件,即所谓的 `next()` 方法,先执行第一个
compose(middlewares) {
return ctx => {
const useMiddleware = i => {
let fn = middlewares[i] //遍历中间件集合
if (!fn) {
return
}
return fn(ctx, () => useMiddleware(i + 1)) //递归执行中间件方法,并且传到一下层,"() => useMiddleware(i + 1)"即 "调用的 await next()"
}
return useMiddleware(0)
}
}
// 挂载中间件
use(middleware) {
if (typeof middleware !== 'function') throw new TypeError('middleware must be a function!')
//打包中间件集合,middleware实则是个方法
this.middlewares.push(middleware)
return this
}
// 启动服务器
listen(...args) {
const server = http.createServer(this.callback())
server.listen(...args)
}
}