-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (58 loc) · 2.07 KB
/
index.js
File metadata and controls
68 lines (58 loc) · 2.07 KB
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
const fs = require('fs');
const PATH = require('path');
class Methodjs {
static getInstance() {
if (Methodjs.instance == null) Methodjs.instance = new Methodjs();
return Methodjs.instance;
}
constructor() {
this.tree = {};
this.eco = {};
this.class = Methodjs;
}
register(objectName, options, method) {
if (this.tree[objectName] == null) {
this.registerObject(objectName);
}
options.name = method.name;
options.objectName = objectName;
method.options = options;
this.tree[objectName][options.name] = method;
}
registerRead(objectName, options, method) {
options.type = 'get';
this.register(objectName, options, method);
}
registeWrite(objectName, options, method) {
options.type = 'post';
this.register(object, options, method);
}
registerObject(objectName) {
this.tree[objectName] = {};
}
express(app) {
for (let objectName in this.tree) {
if (this.tree.hasOwnProperty(objectName)) {
let object = this.tree[objectName];
for (let methodName in object) {
if (object.hasOwnProperty(methodName)) {
let method = object[methodName];
app[method.options.type]('/' + objectName + '/' + methodName, (req, res, next) => {
method({ req, res }).then((result) => {
res.send(result);
}).catch(next);
})
}
}
}
}
}
loadTreeFromDir(path) {
let allObjectDirs = fs.readdirSync(path).map((dirPath) => { return PATH.join(path, dirPath); });
allObjectDirs.forEach((objectDirPath) => {
let allFilesPath = fs.readdirSync(objectDirPath).map((filePath) => { return PATH.join(objectDirPath, filePath) })
allFilesPath.forEach((filePath)=>{require(filePath)})
});
}
}
module.exports = Methodjs;