-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress-app.js
101 lines (84 loc) · 2.37 KB
/
express-app.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const http = require('http');
class express {
constructor() {
// 存放 中间件列表
this.routers = {
ALL:[], // app.use(...)
GET: [], // app.get(...)
POST: [] //app.post(...)
}
}
register() {
// 存储中间件的相关信息,
let info = {};
const slice = Array.prototype.slice;
if (typeof arguments[0] === 'string') {
info.path = arguments[0];
// 从第二个参数开始,将数据放入到 stack 的数组中
info.stack = slice.call(arguments, 1);
} else {
info.path = '/';
info.stack = slice.call(arguments, 0);
}
return info;
}
use() {
const info = this.register.apply(this, arguments);
this.routers.ALL.push(info);
}
get() {
const info = this.register.apply(this, arguments);
this.routers.GET.push(info);
}
post() {
const info = this.register.apply(this, arguments);
this.routers.GET.push(info);
}
match(method, url) {
let stack = [];
if (url === '/favicon.ico') return stack;
const curRoutes = [...this.routers.ALL, ...this.routers[method]];
curRoutes.forEach(routerInfo => {
//url = '/api/getuserinfo' 且 routerInfo.path= ‘/’;
//url = '/api/getuserinfo' 且 routerInfo.path= ‘/api’;
//url = '/api/getuserinfo' 且 routerInfo.path= ‘/api/getuserinfo’;
if (url.indexOf(routerInfo.path) === 0) {
stack = [...stack, ...routerInfo.stack];
}
});
return stack;
}
/**
* @param {} req
* @param {} res
* @param {} stack 该请求的执行栈
* 处理核心next 机制
*/
handle(req, res, stack) {
const next = () => {
// 拿到第一个匹配的中间件
const middleware = stack.shift();
// 执行中间件函数
if (middleware) middleware(req, res, next)
}
next();
}
callback() {
return (req, res) => {
// 定义json 方法
res.json = (data) => {
res.setHeader('Centent-type', 'application/json');
res.end(JSON.stringify(data));
}
const { url, method = method.toLowerCase() } = req;
// 该请求的执行栈列表
const resultList = this.match(method, url);
this.handle(req, res, resultList);
}
}
listen(...args) {
const server = http.createServer(this.callback());
server.listen(...args)
}
}
module.exports = () => new express();