@less-is-more/less-js 1.2.33 → 1.2.36
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/redis.js +23 -12
- package/src/router.js +11 -7
package/package.json
CHANGED
package/src/redis.js
CHANGED
|
@@ -14,23 +14,34 @@ module.exports = class Redis {
|
|
|
14
14
|
|
|
15
15
|
static async getInstance() {
|
|
16
16
|
if (!this.#client) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
this.#client = await redis.createClient(params);
|
|
26
|
-
await this.#client.connect();
|
|
27
|
-
} else {
|
|
28
|
-
throw new Error("Redis not init");
|
|
17
|
+
await Redis.connect();
|
|
18
|
+
} else {
|
|
19
|
+
// 旧的实例判断一下是否可以链接,不能就重新建一个
|
|
20
|
+
try {
|
|
21
|
+
await this.#client.ping();
|
|
22
|
+
} catch (e) {
|
|
23
|
+
await Redis.connect();
|
|
29
24
|
}
|
|
30
25
|
}
|
|
31
26
|
return this.#client;
|
|
32
27
|
}
|
|
33
28
|
|
|
29
|
+
static async connect() {
|
|
30
|
+
if (this.#host) {
|
|
31
|
+
let params = {
|
|
32
|
+
socket: { host: this.#host },
|
|
33
|
+
};
|
|
34
|
+
if (this.#password) {
|
|
35
|
+
params.username = this.#user;
|
|
36
|
+
params.password = this.#password;
|
|
37
|
+
}
|
|
38
|
+
this.#client = redis.createClient(params);
|
|
39
|
+
await this.#client.connect();
|
|
40
|
+
} else {
|
|
41
|
+
throw new Error("Redis not init");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
34
45
|
static async exec(name, ...args) {
|
|
35
46
|
let client = await Redis.getInstance();
|
|
36
47
|
args.unshift(name);
|
package/src/router.js
CHANGED
|
@@ -35,7 +35,8 @@ module.exports = class Router {
|
|
|
35
35
|
await targets.before(req, res, context);
|
|
36
36
|
}
|
|
37
37
|
console.log("start main action");
|
|
38
|
-
|
|
38
|
+
// 需要直接调用,不能用中间变量
|
|
39
|
+
const result = await controller[methodName](req, res, context);
|
|
39
40
|
// after
|
|
40
41
|
if (targets.after) {
|
|
41
42
|
console.log("start after action");
|
|
@@ -72,19 +73,22 @@ module.exports = class Router {
|
|
|
72
73
|
const isFile =
|
|
73
74
|
req.headers &&
|
|
74
75
|
JSON.stringify(req.headers).includes("multipart/form-data");
|
|
75
|
-
|
|
76
|
-
req.headers &&
|
|
77
|
-
JSON.stringify(req.headers).includes("application/json");
|
|
76
|
+
const isJson =
|
|
77
|
+
req.headers && JSON.stringify(req.headers).includes("application/json");
|
|
78
78
|
console.log("multipart:", isFile);
|
|
79
79
|
if (req.method == "POST") {
|
|
80
80
|
if (isFile) {
|
|
81
81
|
await this._getFile(req);
|
|
82
82
|
console.log("params:", req.params);
|
|
83
83
|
console.log("files:", req.files);
|
|
84
|
-
}else if(isJson){
|
|
84
|
+
} else if (isJson) {
|
|
85
85
|
const data = await promisify(body)(req);
|
|
86
|
-
console.log("body: " +
|
|
87
|
-
|
|
86
|
+
console.log("body: " + data);
|
|
87
|
+
try {
|
|
88
|
+
req.body = JSON.parse(data);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
console.error("body格式错误", data);
|
|
91
|
+
}
|
|
88
92
|
} else {
|
|
89
93
|
const data = await promisify(body)(req);
|
|
90
94
|
console.log("body: " + decodeURIComponent(data));
|