ming_node 2.2.5 → 2.4.0
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/README.md +11 -1
- package/beforeTest/BaseMapperTest1.js +24 -0
- package/beforeTest/CollectUtilsTest.js +10 -0
- package/beforeTest/MongoDbBaseRestApiTest.js +3 -0
- package/beforeTest/installPluginTest.js +10 -0
- package/index.js +154 -125
- package/module/BaseMapper.js +56 -2
- package/package.json +1 -1
- package/plugins/BaseGraphqlApi/getGraphqlSchema.js +99 -27
- package/plugins/BaseGraphqlApi/index.js +8 -3
- package/plugins/BaseRpcApi/ApiCloudBaseRpcApi.js +3 -2
- package/utils/common/CollectionUtils.js +111 -0
- package/utils/common/DateUtils.js +36 -0
package/index.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
/**
|
2
|
-
* File :
|
2
|
+
* File : index.js
|
3
3
|
* By : Minglie
|
4
4
|
* QQ: 934031452
|
5
5
|
* Date :2021.12.01
|
6
|
-
* version :2.
|
6
|
+
* version :2.4.0
|
7
7
|
*/
|
8
8
|
var http = require('http');
|
9
9
|
var https = require('https');
|
@@ -42,14 +42,18 @@
|
|
42
42
|
M._globle_cacheMap = {}
|
43
43
|
//全局对象缓存
|
44
44
|
M._globle_lib_cacheMap={}
|
45
|
-
|
45
|
+
//全局插件地址缓存
|
46
|
+
M._globle_plugin_url_cacheMap={};
|
47
|
+
//全局插件
|
48
|
+
M._globle_plugin=new Set();
|
49
|
+
M._node_lib_path=process.cwd()+"/.ming_node_cacke";
|
46
50
|
//远程静态资源路径
|
47
51
|
M.remoteStaticPath = "https://minglie.gitee.io/mingpage/static";
|
48
52
|
M.remoteStaticPathEnable = true;
|
49
53
|
//代理服务器配置
|
50
54
|
M.proxyHost = "http://127.0.0.1:8888"
|
51
55
|
M.proxyHost = "";
|
52
|
-
|
56
|
+
M.IO={}
|
53
57
|
M.setModule=function (key,module){
|
54
58
|
M._moduleMap.set(key,module);
|
55
59
|
}
|
@@ -57,6 +61,16 @@
|
|
57
61
|
M._moduleMap.get(key);
|
58
62
|
}
|
59
63
|
|
64
|
+
M.getGloblePlugin=(pluginKey)=>{
|
65
|
+
let plugin=null;
|
66
|
+
M._globle_plugin.forEach(u=>{
|
67
|
+
if(u.key==pluginKey){
|
68
|
+
plugin=u;
|
69
|
+
}
|
70
|
+
})
|
71
|
+
return plugin;
|
72
|
+
}
|
73
|
+
|
60
74
|
|
61
75
|
/**
|
62
76
|
* ----------------------客户端START--------------------------------------------
|
@@ -74,7 +88,7 @@
|
|
74
88
|
}
|
75
89
|
return Object.assign(obj, c1);
|
76
90
|
}
|
77
|
-
|
91
|
+
|
78
92
|
//将对象追加到url上
|
79
93
|
privateObj.appendDataToUrl = function (url, data) {
|
80
94
|
let getData = "";
|
@@ -90,10 +104,10 @@
|
|
90
104
|
let r = url + getData;
|
91
105
|
return r;
|
92
106
|
}
|
93
|
-
|
107
|
+
|
94
108
|
M.get = function (url, callback, data, headers) {
|
95
109
|
if (typeof callback == "function") {
|
96
|
-
|
110
|
+
|
97
111
|
} else {
|
98
112
|
headers = data || {};
|
99
113
|
data = callback;
|
@@ -139,7 +153,7 @@ M.get = function (url, callback, data, headers) {
|
|
139
153
|
if (url.startsWith("https")) {
|
140
154
|
reqHttp = https;
|
141
155
|
}
|
142
|
-
|
156
|
+
|
143
157
|
return new Promise((resolve, reject) => {
|
144
158
|
options = M.httpBefore(options);
|
145
159
|
if (options == false) {
|
@@ -172,21 +186,21 @@ M.get = function (url, callback, data, headers) {
|
|
172
186
|
req.on('error', function (err) {
|
173
187
|
reject(err);
|
174
188
|
console.error(err);
|
175
|
-
|
189
|
+
|
176
190
|
});
|
177
191
|
req.end();
|
178
192
|
})
|
179
193
|
}
|
180
194
|
M._request = function (url, callback, data, headers,methed) {
|
181
195
|
if (typeof callback == "function") {
|
182
|
-
|
196
|
+
|
183
197
|
} else {
|
184
198
|
headers = data || {};
|
185
199
|
data = callback;
|
186
200
|
callback = () => {
|
187
201
|
};
|
188
202
|
}
|
189
|
-
|
203
|
+
|
190
204
|
url = privateObj.appendDataToUrl(url, M.reqComQueryparams);
|
191
205
|
var html = '';
|
192
206
|
var urlObj = url_module.parse(url)
|
@@ -224,7 +238,7 @@ M._request = function (url, callback, data, headers,methed) {
|
|
224
238
|
if (url.startsWith("https")) {
|
225
239
|
reqHttp = https;
|
226
240
|
}
|
227
|
-
|
241
|
+
|
228
242
|
return new Promise((resolve, reject) => {
|
229
243
|
var req = reqHttp.request(options, function (res) {
|
230
244
|
options = M.httpBefore(options);
|
@@ -254,9 +268,9 @@ M._request = function (url, callback, data, headers,methed) {
|
|
254
268
|
M.httpEnd(html);
|
255
269
|
resolve(html);
|
256
270
|
});
|
257
|
-
|
271
|
+
|
258
272
|
});
|
259
|
-
|
273
|
+
|
260
274
|
req.on('error', function (err) {
|
261
275
|
console.error(err);
|
262
276
|
});
|
@@ -269,7 +283,7 @@ M.delete=(url, callback, data, headers)=>M._request(url, callback, data, headers
|
|
269
283
|
M.put=(url, callback, data, headers)=>M._request(url, callback, data, headers,"PUT")
|
270
284
|
M.postJson = function (url, callback, data, headers) {
|
271
285
|
if (typeof callback == "function") {
|
272
|
-
|
286
|
+
|
273
287
|
} else {
|
274
288
|
headers = data || {};
|
275
289
|
data = callback;
|
@@ -307,9 +321,9 @@ M.postJson = function (url, callback, data, headers) {
|
|
307
321
|
if (url.startsWith("https")) {
|
308
322
|
reqHttp = https;
|
309
323
|
}
|
310
|
-
|
324
|
+
|
311
325
|
return new Promise((resolve, reject) => {
|
312
|
-
|
326
|
+
|
313
327
|
var req = reqHttp.request(options, function (res) {
|
314
328
|
options = M.httpBefore(options);
|
315
329
|
if (options == false) {
|
@@ -338,9 +352,9 @@ M.postJson = function (url, callback, data, headers) {
|
|
338
352
|
M.httpEnd(html);
|
339
353
|
resolve(html);
|
340
354
|
});
|
341
|
-
|
355
|
+
|
342
356
|
});
|
343
|
-
|
357
|
+
|
344
358
|
req.on('error', function (err) {
|
345
359
|
console.error(err);
|
346
360
|
});
|
@@ -397,7 +411,7 @@ M.request.put=M.put;
|
|
397
411
|
});
|
398
412
|
return promise;
|
399
413
|
}
|
400
|
-
|
414
|
+
|
401
415
|
M.import=async function (url,callback){
|
402
416
|
if(M._globle_lib_cacheMap[url]){
|
403
417
|
return M._globle_lib_cacheMap[url];
|
@@ -412,9 +426,9 @@ M.request.put=M.put;
|
|
412
426
|
M._globle_lib_cacheMap[url]=r;
|
413
427
|
return r
|
414
428
|
}
|
415
|
-
|
429
|
+
|
416
430
|
}
|
417
|
-
|
431
|
+
|
418
432
|
/**
|
419
433
|
*下载图片
|
420
434
|
*/
|
@@ -463,23 +477,23 @@ M.request.put=M.put;
|
|
463
477
|
});
|
464
478
|
req.end();
|
465
479
|
}
|
466
|
-
|
480
|
+
|
467
481
|
/**
|
468
482
|
*打印结果前钩子
|
469
483
|
*/
|
470
484
|
M.beforeLogData = function (res, desc) {
|
471
485
|
console.log("-----" + desc + "-----" + res.req.path + "-------------");
|
472
486
|
}
|
473
|
-
|
474
|
-
|
487
|
+
|
488
|
+
|
475
489
|
/**
|
476
490
|
*打印结果后钩子
|
477
491
|
*/
|
478
492
|
M.afterLogData = function () {
|
479
|
-
|
493
|
+
|
480
494
|
console.log("--END")
|
481
495
|
}
|
482
|
-
|
496
|
+
|
483
497
|
/**
|
484
498
|
*简化get请求
|
485
499
|
*/
|
@@ -502,9 +516,9 @@ M.request.put=M.put;
|
|
502
516
|
}, data
|
503
517
|
);
|
504
518
|
}
|
505
|
-
|
519
|
+
|
506
520
|
}
|
507
|
-
|
521
|
+
|
508
522
|
/**
|
509
523
|
*简化post请求
|
510
524
|
*/
|
@@ -516,7 +530,7 @@ M.request.put=M.put;
|
|
516
530
|
}, data
|
517
531
|
);
|
518
532
|
}
|
519
|
-
|
533
|
+
|
520
534
|
M.postJson0 = function (url, data) {
|
521
535
|
M.postJson(
|
522
536
|
M.host + url,
|
@@ -525,28 +539,28 @@ M.request.put=M.put;
|
|
525
539
|
}, data
|
526
540
|
);
|
527
541
|
}
|
528
|
-
|
542
|
+
|
529
543
|
M.template = function (str) {
|
530
544
|
return eval("`" + str + "`");
|
531
545
|
}
|
532
|
-
|
533
|
-
|
546
|
+
|
547
|
+
|
534
548
|
/**
|
535
549
|
* ----------------------客户端END--------------------------------------------
|
536
550
|
*/
|
537
|
-
|
538
|
-
|
551
|
+
|
552
|
+
|
539
553
|
/**
|
540
554
|
* ----------------------数据持久化读写START--------------------------------------------
|
541
555
|
*/
|
542
|
-
|
556
|
+
|
543
557
|
/**
|
544
558
|
*递归创建文件夹
|
545
559
|
*/
|
546
560
|
M.mkdir = function (dirpath, dirname) {
|
547
561
|
//判断是否是第一次调用
|
548
562
|
if (typeof dirname === "undefined") {
|
549
|
-
|
563
|
+
|
550
564
|
if (dirpath.indexOf(".") > 0) {
|
551
565
|
dirpath = path.dirname(dirpath);
|
552
566
|
}
|
@@ -589,7 +603,7 @@ M.request.put=M.put;
|
|
589
603
|
});
|
590
604
|
});
|
591
605
|
}
|
592
|
-
|
606
|
+
|
593
607
|
privateObj.checkDirectory = function (src, dst, callback) {
|
594
608
|
fs.access(dst, fs.constants.F_OK, (err) => {
|
595
609
|
if (err) {
|
@@ -600,7 +614,7 @@ M.request.put=M.put;
|
|
600
614
|
}
|
601
615
|
});
|
602
616
|
};
|
603
|
-
|
617
|
+
|
604
618
|
M.readFile = function (file) {
|
605
619
|
if (fs.existsSync(file)) {
|
606
620
|
return fs.readFileSync(file, "utf-8");
|
@@ -625,7 +639,7 @@ M.request.put=M.put;
|
|
625
639
|
M.writeObjToFile = function (file, obj) {
|
626
640
|
M.writeFile(file, JSON.stringify(obj));
|
627
641
|
}
|
628
|
-
|
642
|
+
|
629
643
|
M.addObjToFile = function (file, obj) {
|
630
644
|
try {
|
631
645
|
var d = M.getObjByFile(file);
|
@@ -653,7 +667,7 @@ M.request.put=M.put;
|
|
653
667
|
}
|
654
668
|
M.writeObjToFile(file, d);
|
655
669
|
}
|
656
|
-
|
670
|
+
|
657
671
|
M.deleteObjByPropFile = function (file, o) {
|
658
672
|
let o_key = Object.keys(o)[0];
|
659
673
|
let o_val = o[o_key]
|
@@ -669,7 +683,7 @@ M.request.put=M.put;
|
|
669
683
|
M.writeObjToFile(file, d);
|
670
684
|
return d_num;
|
671
685
|
}
|
672
|
-
|
686
|
+
|
673
687
|
M.updateObjByIdFile = function (file, obj) {
|
674
688
|
var d = M.getObjByFile(file);
|
675
689
|
for (let i = 0; i < d.length; i++) {
|
@@ -777,10 +791,10 @@ M.request.put=M.put;
|
|
777
791
|
} catch (e) {
|
778
792
|
preObj = {};
|
779
793
|
}
|
780
|
-
|
794
|
+
|
781
795
|
M.writeObjToFile(M.map_path, Object.assign(preObj, a));
|
782
796
|
}
|
783
|
-
|
797
|
+
|
784
798
|
M.getAttribute = function (k) {
|
785
799
|
return M.getObjByFile(M.map_path)[k];
|
786
800
|
}
|
@@ -811,14 +825,14 @@ M.request.put=M.put;
|
|
811
825
|
});
|
812
826
|
} )
|
813
827
|
}
|
814
|
-
|
815
|
-
|
828
|
+
|
829
|
+
|
816
830
|
M.readCsvLine =async function (file, callback) {
|
817
831
|
return M.readLine(file, function (line) {
|
818
832
|
callback(line.replace("\r", "").split(/(?<!\"[^,]+),(?![^,]+\")/));
|
819
833
|
})
|
820
834
|
}
|
821
|
-
|
835
|
+
|
822
836
|
M.getFileNameByUrl=function (url){
|
823
837
|
let split= url.split("/");
|
824
838
|
return split[split.length-1]
|
@@ -829,7 +843,7 @@ M.getFileList = function (path) {
|
|
829
843
|
function readFile(path, filesList, targetObj) {
|
830
844
|
files = fs.readdirSync(path);//需要用到同步读取
|
831
845
|
files.forEach(walk);
|
832
|
-
|
846
|
+
|
833
847
|
function walk(file) {
|
834
848
|
states = fs.statSync(path + '/' + file);
|
835
849
|
if (states.isDirectory()) {
|
@@ -841,7 +855,7 @@ M.getFileList = function (path) {
|
|
841
855
|
item = {name: file, children: [], value: path + '/' + file};
|
842
856
|
filesList.push(item);
|
843
857
|
}
|
844
|
-
|
858
|
+
|
845
859
|
readFile(path + '/' + file, filesList, item);
|
846
860
|
} else {
|
847
861
|
//创建一个对象保存信息
|
@@ -849,7 +863,7 @@ M.getFileList = function (path) {
|
|
849
863
|
obj.size = states.size;//文件大小,以字节为单位
|
850
864
|
obj.name = file;//文件名
|
851
865
|
obj.path = path + '/' + file; //文件绝对路径
|
852
|
-
|
866
|
+
|
853
867
|
if (targetObj["children"]) {
|
854
868
|
var item = {name: file, value: obj.path}
|
855
869
|
targetObj["children"].push(item);
|
@@ -860,7 +874,7 @@ M.getFileList = function (path) {
|
|
860
874
|
}
|
861
875
|
}
|
862
876
|
}
|
863
|
-
|
877
|
+
|
864
878
|
var filesList = [];
|
865
879
|
var targetObj = {};
|
866
880
|
readFile(path, filesList, targetObj);
|
@@ -939,8 +953,8 @@ M.log = function (...params) {
|
|
939
953
|
if (M.log_file_enable) M.appendFile(M.log_path, r);
|
940
954
|
}
|
941
955
|
}
|
942
|
-
|
943
|
-
|
956
|
+
|
957
|
+
|
944
958
|
M.getSqlite = function (dbName) {
|
945
959
|
if (M.sqlite) {
|
946
960
|
return M.sqlite;
|
@@ -978,7 +992,7 @@ M.log = function (...params) {
|
|
978
992
|
M.sqlite = Db;
|
979
993
|
return Db;
|
980
994
|
}
|
981
|
-
|
995
|
+
|
982
996
|
///////////////////////////////
|
983
997
|
|
984
998
|
|
@@ -1089,12 +1103,12 @@ M.getMongoDB = function (dbConfig) {
|
|
1089
1103
|
var MongoDB=require('mongodb');
|
1090
1104
|
var MongoClient =MongoDB.MongoClient;
|
1091
1105
|
const ObjectID = MongoDB.ObjectID;
|
1092
|
-
|
1106
|
+
|
1093
1107
|
var Config={
|
1094
1108
|
dbUrl: dbConfig.dbUrl|| 'mongodb://localhost:27017/',
|
1095
1109
|
dbName: dbConfig.dbName|| 'miapi'
|
1096
1110
|
};
|
1097
|
-
|
1111
|
+
|
1098
1112
|
class MingMongoClient{
|
1099
1113
|
static connect(){
|
1100
1114
|
return new Promise((resolve,reject)=>{
|
@@ -1113,7 +1127,7 @@ M.getMongoDB = function (dbConfig) {
|
|
1113
1127
|
}
|
1114
1128
|
})
|
1115
1129
|
}
|
1116
|
-
|
1130
|
+
|
1117
1131
|
static find(collectionName,json){
|
1118
1132
|
if(!json){
|
1119
1133
|
json=collectionName
|
@@ -1129,7 +1143,7 @@ M.getMongoDB = function (dbConfig) {
|
|
1129
1143
|
}
|
1130
1144
|
resolve(docs);
|
1131
1145
|
})
|
1132
|
-
|
1146
|
+
|
1133
1147
|
})
|
1134
1148
|
})
|
1135
1149
|
}
|
@@ -1164,14 +1178,14 @@ M.getMongoDB = function (dbConfig) {
|
|
1164
1178
|
if(err){
|
1165
1179
|
reject(err);
|
1166
1180
|
}else{
|
1167
|
-
|
1181
|
+
|
1168
1182
|
resolve(result);
|
1169
1183
|
}
|
1170
1184
|
})
|
1171
1185
|
})
|
1172
1186
|
})
|
1173
1187
|
}
|
1174
|
-
|
1188
|
+
|
1175
1189
|
static insertMany(collectionName,json){
|
1176
1190
|
if(!json){
|
1177
1191
|
json=collectionName
|
@@ -1183,14 +1197,14 @@ M.getMongoDB = function (dbConfig) {
|
|
1183
1197
|
if(err){
|
1184
1198
|
reject(err);
|
1185
1199
|
}else{
|
1186
|
-
|
1200
|
+
|
1187
1201
|
resolve(result);
|
1188
1202
|
}
|
1189
1203
|
})
|
1190
1204
|
})
|
1191
1205
|
})
|
1192
1206
|
}
|
1193
|
-
|
1207
|
+
|
1194
1208
|
static remove(collectionName,json){
|
1195
1209
|
if(!json){
|
1196
1210
|
json=collectionName
|
@@ -1202,14 +1216,14 @@ M.getMongoDB = function (dbConfig) {
|
|
1202
1216
|
if(err){
|
1203
1217
|
reject(err);
|
1204
1218
|
}else{
|
1205
|
-
|
1219
|
+
|
1206
1220
|
resolve(result);
|
1207
1221
|
}
|
1208
1222
|
})
|
1209
1223
|
})
|
1210
1224
|
})
|
1211
1225
|
}
|
1212
|
-
|
1226
|
+
|
1213
1227
|
static getById(collectionName,id){
|
1214
1228
|
if(!id){
|
1215
1229
|
id=collectionName
|
@@ -1230,7 +1244,7 @@ M.getMongoDB = function (dbConfig) {
|
|
1230
1244
|
})
|
1231
1245
|
})
|
1232
1246
|
}
|
1233
|
-
|
1247
|
+
|
1234
1248
|
}
|
1235
1249
|
MingMongoClient.ObjectID=(id)=> new ObjectID(id)
|
1236
1250
|
let Db=MingMongoClient;
|
@@ -1239,9 +1253,9 @@ M.getMongoDB = function (dbConfig) {
|
|
1239
1253
|
M.mongoDb=Db;
|
1240
1254
|
return Db;
|
1241
1255
|
}
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1256
|
+
|
1257
|
+
|
1258
|
+
|
1245
1259
|
/**
|
1246
1260
|
* ----------------------Sql CRUD START-------------------------------------------
|
1247
1261
|
*/
|
@@ -1259,7 +1273,7 @@ M.getMongoDB = function (dbConfig) {
|
|
1259
1273
|
let sql = "insert into " + tableName + fields + " values " + values;
|
1260
1274
|
return sql;
|
1261
1275
|
}
|
1262
|
-
|
1276
|
+
|
1263
1277
|
M.getDeleteObjSql = function (tableName, obj) {
|
1264
1278
|
var fields = [];
|
1265
1279
|
for (let field in obj) {
|
@@ -1269,7 +1283,7 @@ M.getMongoDB = function (dbConfig) {
|
|
1269
1283
|
sql = sql.replace(/,/g, " and ")
|
1270
1284
|
return sql;
|
1271
1285
|
}
|
1272
|
-
|
1286
|
+
|
1273
1287
|
M.getUpdateObjSql = function (tableName, obj, caseObj) {
|
1274
1288
|
var fields = [];
|
1275
1289
|
for (let field in obj) {
|
@@ -1286,11 +1300,11 @@ M.getMongoDB = function (dbConfig) {
|
|
1286
1300
|
}
|
1287
1301
|
sql = `update ${tableName} set ${fields.map(u => u + "='" + obj[u] + "'")} where ${caseObjfields.map(u => u + "='" + caseObj[u] + "'").join(" and ")}`;
|
1288
1302
|
}
|
1289
|
-
|
1303
|
+
|
1290
1304
|
return sql;
|
1291
1305
|
}
|
1292
|
-
|
1293
|
-
|
1306
|
+
|
1307
|
+
|
1294
1308
|
M.getSelectObjSql = function (tableName, obj) {
|
1295
1309
|
var fields = [];
|
1296
1310
|
for (let field in obj) {
|
@@ -1300,17 +1314,17 @@ M.getMongoDB = function (dbConfig) {
|
|
1300
1314
|
sql = sql.replace(/,/g, " and ")
|
1301
1315
|
return sql;
|
1302
1316
|
}
|
1303
|
-
|
1317
|
+
|
1304
1318
|
/**
|
1305
1319
|
* ----------------------Sql CRUD START-------------------------------------------
|
1306
1320
|
*/
|
1307
|
-
|
1308
|
-
|
1321
|
+
|
1322
|
+
|
1309
1323
|
/**
|
1310
1324
|
* ----------------------数据持久化读写END--------------------------------------------
|
1311
1325
|
*/
|
1312
|
-
|
1313
|
-
|
1326
|
+
|
1327
|
+
|
1314
1328
|
/**
|
1315
1329
|
* ----------------------服务器端START--------------------------------------------
|
1316
1330
|
*/
|
@@ -1367,7 +1381,7 @@ M.failResult=(msg,code,d)=>{
|
|
1367
1381
|
}
|
1368
1382
|
return result;
|
1369
1383
|
}
|
1370
|
-
|
1384
|
+
|
1371
1385
|
/**
|
1372
1386
|
*获取驼峰式的对象
|
1373
1387
|
*/
|
@@ -1378,7 +1392,7 @@ M.failResult=(msg,code,d)=>{
|
|
1378
1392
|
}
|
1379
1393
|
return result;
|
1380
1394
|
}
|
1381
|
-
|
1395
|
+
|
1382
1396
|
M.randomStr = function () {
|
1383
1397
|
return (Math.random().toString(36) + new Date().getTime()).slice(2);
|
1384
1398
|
}
|
@@ -1416,7 +1430,7 @@ M.urlParse = function (url) {
|
|
1416
1430
|
}
|
1417
1431
|
return s
|
1418
1432
|
};
|
1419
|
-
|
1433
|
+
|
1420
1434
|
/**
|
1421
1435
|
* 异常处理钩子
|
1422
1436
|
* @param e
|
@@ -1428,9 +1442,9 @@ M.urlParse = function (url) {
|
|
1428
1442
|
}
|
1429
1443
|
return true;
|
1430
1444
|
}
|
1431
|
-
|
1432
|
-
|
1433
|
-
|
1445
|
+
|
1446
|
+
|
1447
|
+
|
1434
1448
|
M.server = function () {
|
1435
1449
|
var G = this; /*全局变量,也就是M*/
|
1436
1450
|
//静态资源路径
|
@@ -1481,7 +1495,7 @@ M.urlParse = function (url) {
|
|
1481
1495
|
}
|
1482
1496
|
return isRest;
|
1483
1497
|
}
|
1484
|
-
|
1498
|
+
|
1485
1499
|
req.ip = req.headers['x-forwarded-for'] ||
|
1486
1500
|
req.connection.remoteAddress ||
|
1487
1501
|
req.socket.remoteAddress ||
|
@@ -1557,7 +1571,7 @@ M.urlParse = function (url) {
|
|
1557
1571
|
res.end();
|
1558
1572
|
G._end(req,text);
|
1559
1573
|
}
|
1560
|
-
|
1574
|
+
|
1561
1575
|
res.render = async function (url) {
|
1562
1576
|
res.alreadySend = true;
|
1563
1577
|
let text="";
|
@@ -1705,12 +1719,12 @@ M.urlParse = function (url) {
|
|
1705
1719
|
M["_gloable_exception_handle"](e,req,res);
|
1706
1720
|
}
|
1707
1721
|
}
|
1708
|
-
|
1709
|
-
|
1722
|
+
|
1723
|
+
|
1710
1724
|
app.begin = function (callback) {
|
1711
1725
|
G._begin = callback;
|
1712
1726
|
}
|
1713
|
-
|
1727
|
+
|
1714
1728
|
app.end = function (callback) {
|
1715
1729
|
G._end = callback;
|
1716
1730
|
}
|
@@ -1732,6 +1746,7 @@ M.urlParse = function (url) {
|
|
1732
1746
|
} else {
|
1733
1747
|
plugin.install(app, args);
|
1734
1748
|
}
|
1749
|
+
M._globle_plugin.add(plugin);
|
1735
1750
|
plugin.installed = true;
|
1736
1751
|
}else {
|
1737
1752
|
if (Array.isArray(url)) {
|
@@ -1746,6 +1761,17 @@ M.urlParse = function (url) {
|
|
1746
1761
|
}
|
1747
1762
|
return app;
|
1748
1763
|
}
|
1764
|
+
|
1765
|
+
app.installPlugin=async function (pluginUrl,constructorParams,pluginParams){
|
1766
|
+
if(M._globle_plugin_url_cacheMap[pluginUrl]){
|
1767
|
+
return
|
1768
|
+
}
|
1769
|
+
M._globle_plugin_url_cacheMap[pluginUrl]=pluginUrl;
|
1770
|
+
const Plugin= await M.require(pluginUrl);
|
1771
|
+
const plugin= new Plugin(constructorParams);
|
1772
|
+
app.use(plugin,pluginParams)
|
1773
|
+
}
|
1774
|
+
|
1749
1775
|
/**
|
1750
1776
|
* 注册get请求
|
1751
1777
|
*/
|
@@ -1756,7 +1782,7 @@ M.urlParse = function (url) {
|
|
1756
1782
|
url = url.substr(0, url.indexOf(":"));
|
1757
1783
|
G._rest[url] = realUrl;
|
1758
1784
|
}
|
1759
|
-
|
1785
|
+
|
1760
1786
|
G._get[url] = callback;
|
1761
1787
|
}
|
1762
1788
|
/**
|
@@ -1803,8 +1829,8 @@ M.urlParse = function (url) {
|
|
1803
1829
|
}
|
1804
1830
|
G._mapping[url] = callback;
|
1805
1831
|
}
|
1806
|
-
|
1807
|
-
|
1832
|
+
|
1833
|
+
|
1808
1834
|
M.formatUrl = function (url) {
|
1809
1835
|
if (!url.endsWith('/')) {
|
1810
1836
|
url = url + '/';
|
@@ -1821,7 +1847,7 @@ M.urlParse = function (url) {
|
|
1821
1847
|
req.url = url;
|
1822
1848
|
app(req, res);
|
1823
1849
|
}
|
1824
|
-
|
1850
|
+
|
1825
1851
|
/**
|
1826
1852
|
*重定向
|
1827
1853
|
*/
|
@@ -1829,12 +1855,12 @@ M.urlParse = function (url) {
|
|
1829
1855
|
res.writeHead(302, {'Content-Type': 'text/html; charset=utf-8', 'Location': url});
|
1830
1856
|
res.end();
|
1831
1857
|
}
|
1832
|
-
|
1858
|
+
|
1833
1859
|
app.set = function (k, v) {
|
1834
1860
|
M["_" + k] = v;
|
1835
1861
|
}
|
1836
|
-
|
1837
|
-
|
1862
|
+
|
1863
|
+
|
1838
1864
|
//全局异常钩子
|
1839
1865
|
app.set("gloable_exception_handle",(err,req,res)=>{
|
1840
1866
|
console.error(err.stack)
|
@@ -1843,12 +1869,12 @@ M.urlParse = function (url) {
|
|
1843
1869
|
res.send(M.result(err.message,false,-1));
|
1844
1870
|
}
|
1845
1871
|
})
|
1846
|
-
|
1872
|
+
|
1847
1873
|
//render异常钩子
|
1848
1874
|
app.set("render_exception_handle",(err,req,res)=>{
|
1849
1875
|
console.error(err.stack)
|
1850
1876
|
})
|
1851
|
-
|
1877
|
+
|
1852
1878
|
//没有对应接口时的处理器
|
1853
1879
|
app.set("no_router_handle",(req,res)=>{
|
1854
1880
|
res.end('no router')
|
@@ -1877,19 +1903,19 @@ M.urlParse = function (url) {
|
|
1877
1903
|
res.end();
|
1878
1904
|
})
|
1879
1905
|
|
1880
|
-
|
1881
|
-
|
1882
1906
|
app.listen = function (port) {
|
1883
1907
|
const server= http.createServer(app).listen(port);
|
1884
1908
|
console.log("listen on port:" + port);
|
1885
1909
|
return server;
|
1886
1910
|
}
|
1911
|
+
|
1912
|
+
global.app=app;
|
1887
1913
|
return app;
|
1888
1914
|
}
|
1889
1915
|
M["_gloable_exception_handle"]=(err)=>{
|
1890
1916
|
console.error(err)
|
1891
1917
|
}
|
1892
|
-
|
1918
|
+
|
1893
1919
|
//异常捕获
|
1894
1920
|
process.on('uncaughtException', function (err) {
|
1895
1921
|
M["_gloable_exception_handle"](err,M.req,M.res);
|
@@ -1898,8 +1924,8 @@ M.urlParse = function (url) {
|
|
1898
1924
|
process.on('unhandledRejection',function(err,promise){
|
1899
1925
|
M["_gloable_exception_handle"](err,M.req,M.res);
|
1900
1926
|
});
|
1901
|
-
|
1902
|
-
|
1927
|
+
|
1928
|
+
|
1903
1929
|
/**
|
1904
1930
|
* 代理服务器start
|
1905
1931
|
*/
|
@@ -1930,7 +1956,7 @@ M.urlParse = function (url) {
|
|
1930
1956
|
})
|
1931
1957
|
})
|
1932
1958
|
}
|
1933
|
-
|
1959
|
+
|
1934
1960
|
M.axios = function (axiosConfig) {
|
1935
1961
|
axiosConfig.headers.host = "";
|
1936
1962
|
var urlObj = url_module.parse(axiosConfig.url)
|
@@ -1960,7 +1986,7 @@ M.urlParse = function (url) {
|
|
1960
1986
|
M.httpEnd(html);
|
1961
1987
|
resolve(html);
|
1962
1988
|
});
|
1963
|
-
|
1989
|
+
|
1964
1990
|
});
|
1965
1991
|
req.on('error', function (err) {
|
1966
1992
|
console.error(err);
|
@@ -1994,7 +2020,7 @@ privateObj.dealUseServer = async function (req, res) {
|
|
1994
2020
|
let fileName = pathname.replace("/", "");
|
1995
2021
|
//获取文件的后缀名
|
1996
2022
|
var extname = path.extname(pathname);
|
1997
|
-
|
2023
|
+
|
1998
2024
|
if (fileName.startsWith("__default_")) {
|
1999
2025
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
2000
2026
|
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
|
@@ -2009,7 +2035,7 @@ privateObj.dealUseServer = async function (req, res) {
|
|
2009
2035
|
if (!res.alreadySend) await res.renderUrl(M.remoteStaticPath + pathname)
|
2010
2036
|
return;
|
2011
2037
|
}
|
2012
|
-
|
2038
|
+
|
2013
2039
|
if (pathname != '/favicon.ico') { /*过滤请求favicon.ico*/
|
2014
2040
|
//文件操作获取 static下面的index.html
|
2015
2041
|
fs.readFile(staticPath + '/' + pathname, function (err, data) {
|
@@ -2033,7 +2059,7 @@ privateObj.dealUseServer = async function (req, res) {
|
|
2033
2059
|
res.end();
|
2034
2060
|
}
|
2035
2061
|
}
|
2036
|
-
|
2062
|
+
|
2037
2063
|
/*SSE SERVER */
|
2038
2064
|
M.sseServer = function () {
|
2039
2065
|
//sse 心跳
|
@@ -2134,12 +2160,12 @@ privateObj.dealUseServer = async function (req, res) {
|
|
2134
2160
|
}
|
2135
2161
|
return app;
|
2136
2162
|
}
|
2137
|
-
|
2163
|
+
|
2138
2164
|
/**
|
2139
2165
|
* ----------------------服务器端END--------------------------------------------
|
2140
2166
|
*/
|
2141
|
-
|
2142
|
-
|
2167
|
+
|
2168
|
+
|
2143
2169
|
/**
|
2144
2170
|
* ----------------------其他工具函数START--------------------------------------------
|
2145
2171
|
*/
|
@@ -2149,11 +2175,11 @@ privateObj.dealUseServer = async function (req, res) {
|
|
2149
2175
|
if (err || stderr) console.error(err, stderr);
|
2150
2176
|
reslove(stdout);
|
2151
2177
|
});
|
2152
|
-
|
2178
|
+
|
2153
2179
|
})
|
2154
2180
|
return promise;
|
2155
2181
|
}
|
2156
|
-
|
2182
|
+
|
2157
2183
|
M.getMyIp = function () {
|
2158
2184
|
var interfaces = require('os').networkInterfaces();
|
2159
2185
|
for (var devName in interfaces) {
|
@@ -2166,17 +2192,17 @@ privateObj.dealUseServer = async function (req, res) {
|
|
2166
2192
|
}
|
2167
2193
|
}
|
2168
2194
|
}
|
2169
|
-
|
2195
|
+
|
2170
2196
|
/**
|
2171
2197
|
*对象转JSON key不用引号括起来,因兼容性不好,所以去掉
|
2172
2198
|
*/
|
2173
|
-
|
2199
|
+
|
2174
2200
|
/**
|
2175
2201
|
M.JSOM_Stringify=function(obj){
|
2176
2202
|
return JSON.stringify(obj).replace(/"(\w+)"(\s*:\s*)/gis, '$1$2');
|
2177
2203
|
}
|
2178
2204
|
*/
|
2179
|
-
|
2205
|
+
|
2180
2206
|
M.sleep = function (numberMillis) {
|
2181
2207
|
var now = new Date();
|
2182
2208
|
var exitTime = now.getTime() + numberMillis;
|
@@ -2198,7 +2224,7 @@ M.delayMs=async function (ms){
|
|
2198
2224
|
/**
|
2199
2225
|
* ----------------------其他工具函数END--------------------------------------------
|
2200
2226
|
*/
|
2201
|
-
|
2227
|
+
|
2202
2228
|
/**
|
2203
2229
|
* 静态资源对应表
|
2204
2230
|
*/
|
@@ -2716,11 +2742,11 @@ M.delayMs=async function (ms){
|
|
2716
2742
|
".conf": "text/conf",
|
2717
2743
|
".sql": "text/sql"
|
2718
2744
|
}
|
2719
|
-
|
2745
|
+
|
2720
2746
|
M.test = function () {
|
2721
2747
|
console.log(privateObj.staticMime[".jssson"] || "aa")
|
2722
2748
|
}
|
2723
|
-
|
2749
|
+
|
2724
2750
|
M.getRemoteCacheByUrl = async function (url) {
|
2725
2751
|
if (url in M._globle_cacheMap) {
|
2726
2752
|
return M._globle_cacheMap[url];
|
@@ -2735,7 +2761,7 @@ M.delayMs=async function (ms){
|
|
2735
2761
|
M._globle_cacheMap[url] = text;
|
2736
2762
|
return text;
|
2737
2763
|
}
|
2738
|
-
|
2764
|
+
|
2739
2765
|
M.init = function () {
|
2740
2766
|
/***
|
2741
2767
|
* 下划线命名转为驼峰命名
|
@@ -2747,7 +2773,7 @@ M.delayMs=async function (ms){
|
|
2747
2773
|
});
|
2748
2774
|
return str;
|
2749
2775
|
}
|
2750
|
-
|
2776
|
+
|
2751
2777
|
/***
|
2752
2778
|
* 驼峰命名转下划线
|
2753
2779
|
*/
|
@@ -2756,7 +2782,7 @@ M.delayMs=async function (ms){
|
|
2756
2782
|
str = this.replace(/([A-Z])/g, "_$1").toLowerCase();
|
2757
2783
|
return str;
|
2758
2784
|
}
|
2759
|
-
|
2785
|
+
|
2760
2786
|
//首字母变大写
|
2761
2787
|
String.prototype.firstChartoUpper = function () {
|
2762
2788
|
return this.replace(/^([a-z])/g, function (word) {
|
@@ -2790,9 +2816,12 @@ M.delayMs=async function (ms){
|
|
2790
2816
|
}
|
2791
2817
|
return fmt;
|
2792
2818
|
}
|
2793
|
-
|
2794
|
-
|
2819
|
+
|
2820
|
+
|
2795
2821
|
}
|
2796
2822
|
M.init();
|
2797
|
-
|
2823
|
+
|
2824
|
+
global.M=M;
|
2825
|
+
global.MIO=M.IO;
|
2826
|
+
|
2798
2827
|
module.exports = M;
|