ming_node 2.9.0 → 2.9.1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +12 -4
- package/beforeTest/MingNodemonUtilsTest.js +3 -0
- package/beforeTest/test.js +1 -1
- package/index.js +63 -17
- package/package.json +1 -1
- package/plugins/BaseRestApi/AbstractBaseRestApi.js +3 -3
- package/plugins/BaseRestApi/ApiCloudBaseRestApi.js +2 -2
- package/plugins/BaseRestApi/FileBaseRestApi.js +1 -1
- package/plugins/BaseRestApi/MemoryBaseRestApi.js +1 -1
- package/plugins/BaseRestApi/MongoDbBaseRestApi.js +1 -1
- package/plugins/BaseRestApi/MysqlBaseRestApi.js +3 -3
- package/plugins/BaseRpcApi/AbstractBaseRpcApi.js +7 -4
- package/plugins/BaseRpcApi/ApiCloudBaseRpcApi.js +2 -2
- package/plugins/BaseRpcApi/FileBaseRpcApi.js +1 -1
- package/plugins/BaseRpcApi/MemoryBaseRpcApi.js +1 -1
- package/plugins/BaseRpcApi/MongoDbBaseRpcApi.js +1 -1
- package/plugins/BaseRpcApi/MysqlBaseRpcApi.js +2 -2
- package/utils/nodemon/restart.js +38 -0
package/README.md
CHANGED
@@ -55,7 +55,7 @@ app.get("/getSession",(req,res)=>{
|
|
55
55
|
```javascript
|
56
56
|
+async function(){
|
57
57
|
M =await new Promise((v)=>require('https').get("https://minglie.github.io/js/ming_node.js",(q)=>{d='';q.on('data',(a)=>d+=a);q.on('end',()=>v(eval(d)))}))
|
58
|
-
|
58
|
+
var app=M.server();
|
59
59
|
app.listen(8888);
|
60
60
|
app.get("/",async (req,res)=>{
|
61
61
|
app.redirect("/index.html",req,res)
|
@@ -110,13 +110,21 @@ app.listen(8888);
|
|
110
110
|
app.get("/baidu",(req,res)=>{
|
111
111
|
console.log(req.params);
|
112
112
|
//响应html文本
|
113
|
-
|
113
|
+
res.renderHtml("hello woed")
|
114
114
|
//响应js文本
|
115
|
-
|
115
|
+
res.renderJs("alert(5)")
|
116
116
|
//响应本地文件
|
117
|
-
|
117
|
+
res.renderUrl("file:D:/G/ming_node/test/test.html");
|
118
118
|
//响应百度首页
|
119
119
|
res.renderUrl("https://www.baidu.com/index.html");
|
120
|
+
//响应一个网络文件,支持二进制
|
121
|
+
res.sendFile("https://ming/a.txt");
|
122
|
+
//响应一个本地文件,支持二进制
|
123
|
+
res.sendFile("file:C:/Users/a.txt");
|
124
|
+
//响应一个网络文件,支持二进制,浏览器直接下载
|
125
|
+
res.sendFile("https://ming/a.txt",true);
|
126
|
+
//响应一个本地文件,支持二进制,浏览器直接下载
|
127
|
+
res.sendFile("file:C:/Users/a.txt",true);
|
120
128
|
})
|
121
129
|
|
122
130
|
```
|
package/beforeTest/test.js
CHANGED
package/index.js
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
* By : Minglie
|
4
4
|
* QQ: 934031452
|
5
5
|
* Date :2021.12.01
|
6
|
-
* version :2.
|
6
|
+
* version :2.9.1
|
7
7
|
*/
|
8
8
|
var http = require('http');
|
9
9
|
var https = require('https');
|
@@ -437,23 +437,22 @@ M.import=async function (url,callback){
|
|
437
437
|
/**
|
438
438
|
*下载图片
|
439
439
|
*/
|
440
|
-
M.download = function (url, file
|
440
|
+
M.download =async function (url, file) {
|
441
441
|
var func = http;
|
442
442
|
if (url.indexOf("https") >= 0) {
|
443
443
|
func = https;
|
444
444
|
}
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
if (err) throw err;
|
454
|
-
});
|
445
|
+
return new Promise(((resolve, reject) => {
|
446
|
+
func.get(url, function (res) {
|
447
|
+
let writeStream = fs.createWriteStream(file);
|
448
|
+
res.on("end",d=>{
|
449
|
+
resolve(file);
|
450
|
+
}).on("error",e=>{
|
451
|
+
reject(e);
|
452
|
+
}).pipe(writeStream);
|
455
453
|
});
|
456
|
-
})
|
454
|
+
}))
|
455
|
+
|
457
456
|
}
|
458
457
|
/**
|
459
458
|
*下载所有图片
|
@@ -823,10 +822,8 @@ M.readLine =async function (file, callback) {
|
|
823
822
|
}
|
824
823
|
});
|
825
824
|
input.on('end', function () {
|
826
|
-
|
827
|
-
|
828
|
-
callback(remaining);
|
829
|
-
}
|
825
|
+
resolve(lineCount);
|
826
|
+
callback(remaining);
|
830
827
|
});
|
831
828
|
} )
|
832
829
|
}
|
@@ -1579,6 +1576,47 @@ M.server = function () {
|
|
1579
1576
|
G._end(req,text);
|
1580
1577
|
}
|
1581
1578
|
|
1579
|
+
|
1580
|
+
res.sendFile= async function (url,isDownLoad=false) {
|
1581
|
+
let extname = path.extname(url);
|
1582
|
+
let fileName=M.getFileNameByUrl(url);
|
1583
|
+
if(url.startsWith("file")){
|
1584
|
+
url= url.substring(5);
|
1585
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
1586
|
+
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
|
1587
|
+
res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
|
1588
|
+
res.setHeader("X-Powered-By", ' 3.2.1');
|
1589
|
+
if(isDownLoad){
|
1590
|
+
res.setHeader("Content-Disposition", "attachment;filename="+encodeURIComponent(fileName,"utf-8"));
|
1591
|
+
}
|
1592
|
+
res.writeHead(200, {"Content-Type": "" + (privateObj.staticMime[extname] || 'text/html') + ";charset='utf-8'",});
|
1593
|
+
fs.createReadStream(url).on("end",d=>{})
|
1594
|
+
.on("error",e=>{
|
1595
|
+
console.error(e);
|
1596
|
+
}).pipe(res);
|
1597
|
+
}else {
|
1598
|
+
var func = http;
|
1599
|
+
if (url.indexOf("https") >= 0) {
|
1600
|
+
func = https;
|
1601
|
+
}
|
1602
|
+
func.get(url, function (res1) {
|
1603
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
1604
|
+
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
|
1605
|
+
res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
|
1606
|
+
res.setHeader("X-Powered-By", ' 3.2.1');
|
1607
|
+
if(isDownLoad){
|
1608
|
+
res.setHeader("Content-Disposition", "attachment;filename="+encodeURIComponent(fileName,"utf-8"));
|
1609
|
+
}
|
1610
|
+
res.writeHead(200, {"Content-Type": "" + (privateObj.staticMime[extname] || 'text/html') + ";charset='utf-8'",});
|
1611
|
+
res1.on("end",d=>{})
|
1612
|
+
.on("error",e=>{
|
1613
|
+
console.error(e)
|
1614
|
+
}).pipe(res);
|
1615
|
+
});
|
1616
|
+
}
|
1617
|
+
}
|
1618
|
+
|
1619
|
+
|
1582
1620
|
res.render = async function (url) {
|
1583
1621
|
res.alreadySend = true;
|
1584
1622
|
let text="";
|
@@ -1628,6 +1666,14 @@ M.server = function () {
|
|
1628
1666
|
res.end();
|
1629
1667
|
G._end(req,text);
|
1630
1668
|
}
|
1669
|
+
//扩充res一个renderHtml方法
|
1670
|
+
res.renderCss = function (text) {
|
1671
|
+
res.alreadySend = true;
|
1672
|
+
res.writeHead(200, {"Content-Type": "text/css;charset='utf-8'"});
|
1673
|
+
res.write(text);
|
1674
|
+
res.end();
|
1675
|
+
G._end(req,text);
|
1676
|
+
}
|
1631
1677
|
res.redirect = function (url) {
|
1632
1678
|
res.alreadySend = true;
|
1633
1679
|
res.writeHead(302, {'Content-Type': 'text/html; charset=utf-8', 'Location': url});
|
package/package.json
CHANGED
@@ -10,7 +10,7 @@ class AbstractBaseRest{
|
|
10
10
|
|
11
11
|
async add(obj){}
|
12
12
|
async delete(obj){}
|
13
|
-
async list({page,num,...queryCase}){}
|
13
|
+
async list({page,num,order,...queryCase}){}
|
14
14
|
async listAll(obj){}
|
15
15
|
async update(obj){}
|
16
16
|
async getChildenList(id,queryCase){}
|
@@ -42,9 +42,9 @@ class AbstractBaseRest{
|
|
42
42
|
});
|
43
43
|
|
44
44
|
app.get(`${this.prefix}/:id`,async (req,res)=>{
|
45
|
-
const {id,page,num,...queryCase}=req.params;
|
45
|
+
const {id,page,num,order,...queryCase}=req.params;
|
46
46
|
if(id==undefined){
|
47
|
-
let r=await this.list({page,num,queryCase});
|
47
|
+
let r=await this.list({page,num,order,queryCase});
|
48
48
|
res.send(M.successResult(r));
|
49
49
|
}else {
|
50
50
|
let r=await this.getById(req.params.id);
|
@@ -27,12 +27,12 @@ class ApiCloudBaseRestApi extends AbstractBaseRestApi{
|
|
27
27
|
return r[0];
|
28
28
|
}
|
29
29
|
|
30
|
-
async list({page=1,num=10,queryCase}){
|
30
|
+
async list({page=1,num=10,order="createdAt ASC",queryCase}){
|
31
31
|
page=Number.parseInt(page);
|
32
32
|
num=Number.parseInt(num);
|
33
33
|
let limit=num;
|
34
34
|
let skip= (page-1)*num
|
35
|
-
let r=await this.tableClient.list(queryCase,limit,skip,
|
35
|
+
let r=await this.tableClient.list(queryCase,limit,skip,order);
|
36
36
|
return r;
|
37
37
|
}
|
38
38
|
|
@@ -30,9 +30,9 @@ class MysqlBaseRestApi extends AbstractBaseRestApi{
|
|
30
30
|
return r;
|
31
31
|
}
|
32
32
|
|
33
|
-
async list({page,num,queryCase}){
|
34
|
-
let queryCaseStr=
|
35
|
-
let r= this.dbBaseMapper.selectPage({page,num,queryCase:queryCaseStr});
|
33
|
+
async list({page,num,order,queryCase}){
|
34
|
+
let queryCaseStr= MysqlBaseRpcApi.obj2QueryCase(queryCase);
|
35
|
+
let r= this.dbBaseMapper.selectPage({order,page,num,queryCase:queryCaseStr});
|
36
36
|
return r;
|
37
37
|
}
|
38
38
|
|
@@ -11,7 +11,7 @@ class AbstractBaseRpcApi{
|
|
11
11
|
|
12
12
|
async add(obj){}
|
13
13
|
async delete(obj){}
|
14
|
-
async list({page,num,...queryCase}){}
|
14
|
+
async list({page,num,order,...queryCase}){}
|
15
15
|
async listAll(obj){}
|
16
16
|
async update(obj){}
|
17
17
|
async getChildenList(id,queryCase){}
|
@@ -51,10 +51,13 @@ class AbstractBaseRpcApi{
|
|
51
51
|
});
|
52
52
|
|
53
53
|
app.get(`${this.prefix}/list`,async (req,res)=>{
|
54
|
-
|
55
|
-
|
54
|
+
let {page,num,order,...queryCase}=req.params;
|
55
|
+
if(!order){
|
56
|
+
order=null;
|
57
|
+
}
|
58
|
+
let r=await this.list({page,num,order,queryCase});
|
56
59
|
res.send(M.successResult(r));
|
57
|
-
})
|
60
|
+
});
|
58
61
|
|
59
62
|
/**
|
60
63
|
* 如果有parent_id才能返回树
|
@@ -27,12 +27,12 @@ class ApiCloudBaseRpcApi extends AbstractBaseRpcApi{
|
|
27
27
|
return r[0];
|
28
28
|
}
|
29
29
|
|
30
|
-
async list({page=1,num=10,queryCase}){
|
30
|
+
async list({page=1,num=10,order="createdAt ASC",queryCase}){
|
31
31
|
page=Number.parseInt(page);
|
32
32
|
num=Number.parseInt(num);
|
33
33
|
let limit=num;
|
34
34
|
let skip= (page-1)*num
|
35
|
-
let rows=await this.tableClient.list(queryCase,limit,skip,
|
35
|
+
let rows=await this.tableClient.list(queryCase,limit,skip,order);
|
36
36
|
let countResult=await this.tableClient.count(queryCase);
|
37
37
|
return {rows, total:countResult.count}
|
38
38
|
}
|
@@ -30,9 +30,9 @@ class MysqlBaseRpcApi extends AbstractBaseRpcApi{
|
|
30
30
|
return r;
|
31
31
|
}
|
32
32
|
|
33
|
-
async list({page,num,queryCase}){
|
33
|
+
async list({page,num,order,queryCase}){
|
34
34
|
let queryCaseStr= MysqlBaseRpcApi.obj2QueryCase(queryCase);
|
35
|
-
let r= this.dbBaseMapper.selectPage({page,num,queryCase:queryCaseStr});
|
35
|
+
let r= this.dbBaseMapper.selectPage({order,page,num,queryCase:queryCaseStr});
|
36
36
|
return r;
|
37
37
|
}
|
38
38
|
|
@@ -0,0 +1,38 @@
|
|
1
|
+
const M = require('../../index.js');
|
2
|
+
const { spawn } = require('child_process')
|
3
|
+
|
4
|
+
let childProcess;
|
5
|
+
let timer = null;
|
6
|
+
|
7
|
+
function debounce(mainFile) {
|
8
|
+
timer && clearTimeout(timer)
|
9
|
+
timer = setTimeout(() => {
|
10
|
+
//console.log('nodemon')
|
11
|
+
childProcess && childProcess.kill()
|
12
|
+
childProcess = spawn('node', [mainFile], {
|
13
|
+
stdio: [process.stdin, process.stdout, process.stderr]
|
14
|
+
});
|
15
|
+
}, 800)
|
16
|
+
}
|
17
|
+
|
18
|
+
function watchSigleFile(file,mainFile){
|
19
|
+
M.watchFile(file,({file,event})=>{
|
20
|
+
if(event=="change"){
|
21
|
+
console.log("change:"+file);
|
22
|
+
debounce(mainFile)
|
23
|
+
}
|
24
|
+
})
|
25
|
+
}
|
26
|
+
|
27
|
+
module.exports = function (watchFile,mainFile){
|
28
|
+
debounce(mainFile);
|
29
|
+
let r=[];
|
30
|
+
if(Array.isArray(watchFile)) {
|
31
|
+
r=watchFile;
|
32
|
+
}else {
|
33
|
+
r.push(watchFile);
|
34
|
+
}
|
35
|
+
r.forEach(u=>{
|
36
|
+
watchSigleFile(u,mainFile);
|
37
|
+
})
|
38
|
+
};
|