@less-is-more/less-js 1.2.31 → 1.2.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/router.js +15 -1
- package/test/test-router.js +209 -201
package/package.json
CHANGED
package/src/router.js
CHANGED
|
@@ -20,7 +20,11 @@ module.exports = class Router {
|
|
|
20
20
|
const startTime = new Date().getTime();
|
|
21
21
|
const { methodName, className } = this._getPath(req);
|
|
22
22
|
if (methodName) {
|
|
23
|
-
const
|
|
23
|
+
const controller = targets[className];
|
|
24
|
+
if (controller == undefined) {
|
|
25
|
+
return this._sendError("请输入正确路径", res);
|
|
26
|
+
}
|
|
27
|
+
const method = controller[methodName];
|
|
24
28
|
if (method) {
|
|
25
29
|
try {
|
|
26
30
|
// 兼容非阿里云
|
|
@@ -68,12 +72,22 @@ module.exports = class Router {
|
|
|
68
72
|
const isFile =
|
|
69
73
|
req.headers &&
|
|
70
74
|
JSON.stringify(req.headers).includes("multipart/form-data");
|
|
75
|
+
const isJson =
|
|
76
|
+
req.headers && JSON.stringify(req.headers).includes("application/json");
|
|
71
77
|
console.log("multipart:", isFile);
|
|
72
78
|
if (req.method == "POST") {
|
|
73
79
|
if (isFile) {
|
|
74
80
|
await this._getFile(req);
|
|
75
81
|
console.log("params:", req.params);
|
|
76
82
|
console.log("files:", req.files);
|
|
83
|
+
} else if (isJson) {
|
|
84
|
+
const data = await promisify(body)(req);
|
|
85
|
+
console.log("body: " + data);
|
|
86
|
+
try {
|
|
87
|
+
req.body = JSON.parse(data);
|
|
88
|
+
} catch (e) {
|
|
89
|
+
console.error("body格式错误", data);
|
|
90
|
+
}
|
|
77
91
|
} else {
|
|
78
92
|
const data = await promisify(body)(req);
|
|
79
93
|
console.log("body: " + decodeURIComponent(data));
|
package/test/test-router.js
CHANGED
|
@@ -3,205 +3,213 @@ const Router = require("../src/router.js");
|
|
|
3
3
|
const Ret = require("../src/ret");
|
|
4
4
|
|
|
5
5
|
describe("router.js", () => {
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
6
|
+
describe("route()", () => {
|
|
7
|
+
it("have function", () => {
|
|
8
|
+
let targets = {
|
|
9
|
+
"/": {
|
|
10
|
+
add: function (req, res) {
|
|
11
|
+
res.send("ok");
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
let req = { path: "/add" };
|
|
16
|
+
let res = {
|
|
17
|
+
send: function (data) {
|
|
18
|
+
console.log("send: " + data);
|
|
19
|
+
assert.deepStrictEqual(data, "ok");
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
Router.route(targets, req, res);
|
|
23
|
+
});
|
|
24
|
+
it("no function", () => {
|
|
25
|
+
let targets = {
|
|
26
|
+
"/": {
|
|
27
|
+
add: function (req, res) {
|
|
28
|
+
res.send("ok");
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
let req = { path: "/add2" };
|
|
33
|
+
let res = {
|
|
34
|
+
send: function (data) {
|
|
35
|
+
assert(JSON.parse(data).success == false);
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
Router.route(targets, req, res);
|
|
39
|
+
});
|
|
40
|
+
it("no path", () => {
|
|
41
|
+
let targets = {};
|
|
42
|
+
let req = { path: "/fav.ico" };
|
|
43
|
+
let res = {
|
|
44
|
+
send: function (data) {
|
|
45
|
+
assert(JSON.parse(data).success == false);
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
Router.route(targets, req, res);
|
|
49
|
+
});
|
|
50
|
+
it("error", () => {
|
|
51
|
+
let targets = {
|
|
52
|
+
"/": {
|
|
53
|
+
add: function (req, res) {
|
|
54
|
+
throw new Error("出错啦");
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
let req = { path: "/add" };
|
|
59
|
+
let res = {
|
|
60
|
+
send: function (data) {
|
|
61
|
+
console.log("send: " + data);
|
|
62
|
+
assert(JSON.parse(data).success == false);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
Router.route(targets, req, res);
|
|
66
|
+
});
|
|
67
|
+
it("multi controllers", () => {
|
|
68
|
+
let targets = {
|
|
69
|
+
"/menu": {
|
|
70
|
+
add: function (req, res) {
|
|
71
|
+
res.send(new Ret(true).toString());
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
let req = { path: "/menu/add" };
|
|
76
|
+
let res = {
|
|
77
|
+
send: function (data) {
|
|
78
|
+
console.log("send: " + data);
|
|
79
|
+
assert(JSON.parse(data).success);
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
Router.route(targets, req, res);
|
|
83
|
+
});
|
|
84
|
+
it("no default error format", async () => {
|
|
85
|
+
let targets = {
|
|
86
|
+
"/": {
|
|
87
|
+
add: function (req, res) {
|
|
88
|
+
throw new Error("出错啦");
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
let req = { path: "/add" };
|
|
93
|
+
let res = {
|
|
94
|
+
send: function (data) {
|
|
95
|
+
console.log("send: " + data);
|
|
96
|
+
assert(false);
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
Router.useDefaultFormat(false);
|
|
100
|
+
let result = await Router.route(targets, req, res);
|
|
101
|
+
console.log("result", result);
|
|
102
|
+
assert(result.indexOf("出错啦") >= 0);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
it("return value for send", async () => {
|
|
106
|
+
let targets = {
|
|
107
|
+
"/": {
|
|
108
|
+
add: function (req, res) {
|
|
109
|
+
return 1;
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
let req = { path: "/add" };
|
|
114
|
+
let res = {
|
|
115
|
+
send: function (data) {
|
|
116
|
+
console.log("send: " + data);
|
|
117
|
+
assert(data === "1");
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
await Router.route(targets, req, res);
|
|
121
|
+
});
|
|
122
|
+
it("return string for send", async () => {
|
|
123
|
+
let targets = {
|
|
124
|
+
"/": {
|
|
125
|
+
add: function (req, res) {
|
|
126
|
+
return JSON.stringify({
|
|
127
|
+
code: 1000,
|
|
128
|
+
success: false,
|
|
129
|
+
message: "请输入发货类型",
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
let req = { path: "/add" };
|
|
135
|
+
let res = {
|
|
136
|
+
send: function (data) {
|
|
137
|
+
console.log("send: " + data);
|
|
138
|
+
assert(
|
|
139
|
+
data === '{"code":1000,"success":false,"message":"请输入发货类型"}'
|
|
140
|
+
);
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
await Router.route(targets, req, res);
|
|
144
|
+
});
|
|
145
|
+
it("return object for send", async () => {
|
|
146
|
+
let targets = {
|
|
147
|
+
"/": {
|
|
148
|
+
add: function (req, res) {
|
|
149
|
+
return { code: 1000, success: false, message: "请输入发货类型" };
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
let req = { path: "/add" };
|
|
154
|
+
let res = {
|
|
155
|
+
send: function (data) {
|
|
156
|
+
console.log("send: " + data);
|
|
157
|
+
assert(
|
|
158
|
+
data === '{"code":1000,"success":false,"message":"请输入发货类型"}'
|
|
159
|
+
);
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
await Router.route(targets, req, res);
|
|
163
|
+
});
|
|
164
|
+
it("return Ret for send", async () => {
|
|
165
|
+
let targets = {
|
|
166
|
+
"/": {
|
|
167
|
+
add: function (req, res) {
|
|
168
|
+
return new Ret(true);
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
let req = { path: "/add" };
|
|
173
|
+
let res = {
|
|
174
|
+
send: function (data) {
|
|
175
|
+
console.log("send: " + data);
|
|
176
|
+
assert(data === '{"success":true}');
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
await Router.route(targets, req, res);
|
|
180
|
+
});
|
|
181
|
+
it("request params", async () => {
|
|
182
|
+
let targets = {
|
|
183
|
+
"/product": {
|
|
184
|
+
add: function (req, res) {
|
|
185
|
+
assert(req.params.a === 1);
|
|
186
|
+
return 1;
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
let req = { method: "GET", path: "/product/add", queries: { a: 1 } };
|
|
191
|
+
let res = {
|
|
192
|
+
send: function (data) {
|
|
193
|
+
console.log("send: " + data);
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
await Router.route(targets, req, res);
|
|
197
|
+
});
|
|
198
|
+
it("compatible query", async () => {
|
|
199
|
+
let targets = {
|
|
200
|
+
"/product": {
|
|
201
|
+
add: function (req, res) {
|
|
202
|
+
assert(req.params.a === 1);
|
|
203
|
+
return 1;
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
let req = { method: "GET", path: "/product/add", query: { a: 1 } };
|
|
208
|
+
let res = {
|
|
209
|
+
send: function (data) {
|
|
210
|
+
console.log("send: " + data);
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
await Router.route(targets, req, res);
|
|
214
|
+
});
|
|
207
215
|
});
|