chanjs 1.0.11 → 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +15 -1
- package/core/chan.js +45 -42
- package/package.json +1 -1
package/README.md
CHANGED
@@ -34,6 +34,16 @@ Chan.js 基于express 纯js研发的轻量级mvc框架。基于函数式编程
|
|
34
34
|
|- extend 扩展
|
35
35
|
|- middleware 中间件
|
36
36
|
|- plugin 插件
|
37
|
+
|- plus-module1 插件1
|
38
|
+
|- controller 控制器
|
39
|
+
|- service 服务模型
|
40
|
+
|- view 视图模板
|
41
|
+
|- router.js 路由
|
42
|
+
|- module2 插件2
|
43
|
+
|- controller 控制器
|
44
|
+
|- service 服务模型
|
45
|
+
|- view 视图模板
|
46
|
+
|- router.js路由
|
37
47
|
|- public 静态文件
|
38
48
|
|- index.js
|
39
49
|
```
|
@@ -48,6 +58,9 @@ Chan.js 基于express 纯js研发的轻量级mvc框架。基于函数式编程
|
|
48
58
|
- 加载router
|
49
59
|
- 加载extend
|
50
60
|
- 加载plugin
|
61
|
+
- 加载service
|
62
|
+
- 加载controller
|
63
|
+
- 加载router
|
51
64
|
- beforeStart() 挂在从数据库获取的配置合并到配置文件中
|
52
65
|
- run() 启动服务
|
53
66
|
|
@@ -61,7 +74,8 @@ Chan.js 基于express 纯js研发的轻量级mvc框架。基于函数式编程
|
|
61
74
|
|
62
75
|
* 配置文件
|
63
76
|
* 多模块mvc
|
64
|
-
*
|
77
|
+
* 多插件mvc
|
78
|
+
* mysql数据库支持
|
65
79
|
* 路由控制
|
66
80
|
* art-template模板
|
67
81
|
* 静态资源
|
package/core/chan.js
CHANGED
@@ -13,6 +13,8 @@ class Chan {
|
|
13
13
|
Chan.config = {...config,...options};
|
14
14
|
//模块
|
15
15
|
Chan.modules ={};
|
16
|
+
//插件
|
17
|
+
Chan.plugins ={};
|
16
18
|
//工具
|
17
19
|
Chan.helper ={};
|
18
20
|
//应用实例
|
@@ -31,6 +33,10 @@ class Chan {
|
|
31
33
|
this.beforeStart();
|
32
34
|
//加载模块
|
33
35
|
this.loadModules();
|
36
|
+
//加载插件
|
37
|
+
this.loadPlugins();
|
38
|
+
//加载通用路由
|
39
|
+
this.loadCommonRouter();
|
34
40
|
//生命周期:初始化完成
|
35
41
|
this.start()
|
36
42
|
}
|
@@ -109,16 +115,7 @@ class Chan {
|
|
109
115
|
|
110
116
|
// 加载插件
|
111
117
|
loadPlugins() {
|
112
|
-
|
113
|
-
if (fs.existsSync(configPath)) {
|
114
|
-
const dirs = fs
|
115
|
-
.readdirSync(configPath, { withFileTypes: true })
|
116
|
-
.filter((dirent) => dirent.isDirectory())
|
117
|
-
.map((dirent) => dirent.name);
|
118
|
-
this.plugins = dirs;
|
119
|
-
} else {
|
120
|
-
this.plugins = [];
|
121
|
-
}
|
118
|
+
this.loadModules('plugins');
|
122
119
|
}
|
123
120
|
|
124
121
|
/**
|
@@ -131,30 +128,24 @@ class Chan {
|
|
131
128
|
/**
|
132
129
|
* @description 模块加载入口(路由&控制器& 服务)
|
133
130
|
*/
|
134
|
-
loadModules() {
|
135
|
-
const configPath = path.join(config.APP_PATH,
|
131
|
+
loadModules(modules='modules') {
|
132
|
+
const configPath = path.join(config.APP_PATH,modules);
|
136
133
|
if (fs.existsSync(configPath)) {
|
137
134
|
const dirs = fs
|
138
135
|
.readdirSync(configPath, { withFileTypes: true })
|
139
136
|
.filter((dirent) => dirent.isDirectory())
|
140
137
|
.map((dirent) => dirent.name);
|
141
|
-
|
142
|
-
this.modulesDir = dirs;
|
138
|
+
Chan[modules]={};
|
143
139
|
for(let i=0,item;i<dirs.length;i++){
|
144
140
|
item = dirs[i];
|
145
|
-
Chan
|
141
|
+
Chan[modules][item] = {
|
146
142
|
controller: {},
|
147
143
|
service: {},
|
148
144
|
};
|
149
|
-
this.loadModule(item);
|
145
|
+
this.loadModule(modules,item);
|
150
146
|
}
|
151
147
|
|
152
|
-
|
153
|
-
const baseRouterPath = path.join(config.APP_PATH, "router.js");
|
154
|
-
if (fs.existsSync(baseRouterPath)) {
|
155
|
-
const _router = require(baseRouterPath);
|
156
|
-
_router(this.app, this.router);
|
157
|
-
}
|
148
|
+
|
158
149
|
//执行路由
|
159
150
|
// this.app.use(this.router);
|
160
151
|
}
|
@@ -165,11 +156,10 @@ class Chan {
|
|
165
156
|
* @description 加载模块,包括 controller service router
|
166
157
|
* @param {String} moduleName 模块名称
|
167
158
|
*/
|
168
|
-
loadModule(moduleName) {
|
169
|
-
|
170
|
-
this.
|
171
|
-
this.
|
172
|
-
this.loadRoutes(moduleDir,moduleName);
|
159
|
+
loadModule(modules,moduleName) {
|
160
|
+
this.loadServices(modules,moduleName);
|
161
|
+
this.loadControllers(modules,moduleName);
|
162
|
+
this.loadRoutes(modules,moduleName);
|
173
163
|
}
|
174
164
|
|
175
165
|
|
@@ -178,8 +168,9 @@ class Chan {
|
|
178
168
|
* @param {*} moduleDir 模块路径
|
179
169
|
* @param {*} moduleName 模块名称
|
180
170
|
*/
|
181
|
-
loadServices(
|
182
|
-
const servicesDir = path.join(
|
171
|
+
loadServices(modules,moduleName) {
|
172
|
+
const servicesDir = path.join(config.APP_PATH, modules, moduleName,"service");
|
173
|
+
|
183
174
|
if (fs.existsSync(servicesDir)) {
|
184
175
|
let services = fs
|
185
176
|
.readdirSync(servicesDir)
|
@@ -188,8 +179,9 @@ class Chan {
|
|
188
179
|
file= services[i]
|
189
180
|
const Service = require(path.join(servicesDir, file));
|
190
181
|
const serviceName = file.replace(".js", "");
|
191
|
-
|
192
|
-
Chan
|
182
|
+
//模块文件夹-模块文件名-服务-方法
|
183
|
+
Chan[modules][moduleName].service[serviceName] = {};
|
184
|
+
Chan[modules][moduleName].service[serviceName] = Service;
|
193
185
|
}
|
194
186
|
}
|
195
187
|
}
|
@@ -200,8 +192,8 @@ class Chan {
|
|
200
192
|
* @param {*} moduleDir 模块路径
|
201
193
|
* @param {*} moduleName 模块名称
|
202
194
|
*/
|
203
|
-
loadControllers(
|
204
|
-
const controllersDir = path.join(
|
195
|
+
loadControllers(modules,moduleName) {
|
196
|
+
const controllersDir = path.join(config.APP_PATH, modules, moduleName,"controller");
|
205
197
|
if (fs.existsSync(controllersDir)) {
|
206
198
|
let controllers = fs
|
207
199
|
.readdirSync(controllersDir)
|
@@ -210,8 +202,8 @@ class Chan {
|
|
210
202
|
file= controllers[i]
|
211
203
|
const controller = require(path.join(controllersDir, file));
|
212
204
|
const controllerName = file.replace(".js", "");
|
213
|
-
Chan
|
214
|
-
Chan
|
205
|
+
Chan[modules][moduleName].controller[controllerName] = {};
|
206
|
+
Chan[modules][moduleName].controller[controllerName] = controller;
|
215
207
|
}
|
216
208
|
}
|
217
209
|
}
|
@@ -221,19 +213,30 @@ class Chan {
|
|
221
213
|
* @param {*} moduleDir 模块路径
|
222
214
|
* @param {*} moduleName 模块名称
|
223
215
|
*/
|
224
|
-
loadRoutes(
|
225
|
-
const routersDir = path.join(
|
216
|
+
loadRoutes(modules,moduleName) {
|
217
|
+
const routersDir = path.join(config.APP_PATH, modules, moduleName,"router.js");
|
226
218
|
if (fs.existsSync(routersDir)) {
|
227
219
|
const routes = require(routersDir);
|
228
|
-
routes({router:this.router,modules:Chan
|
220
|
+
routes({router:this.router,modules:Chan[modules],app:this.app});
|
229
221
|
}
|
230
222
|
}
|
231
223
|
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
224
|
+
//通用路由,加载错误处理和500路由和爬虫处理
|
225
|
+
loadCommonRouter(){
|
226
|
+
try {
|
227
|
+
const baseRouterPath = path.join(config.APP_PATH, "router.js");
|
228
|
+
if (fs.existsSync(baseRouterPath)) {
|
229
|
+
const _router = require(baseRouterPath);
|
230
|
+
console.log('_router-->',_router);
|
231
|
+
_router(this.app, this.router);
|
232
|
+
}
|
233
|
+
} catch (error) {
|
234
|
+
console.log(error);
|
235
|
+
}
|
236
|
+
|
236
237
|
}
|
238
|
+
|
239
|
+
|
237
240
|
run() {
|
238
241
|
const port = this.app.config.port || '81';
|
239
242
|
this.app.listen(port, () => {
|