chanjs 1.0.11 → 1.0.13
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 +40 -44
- package/package.json +4 -4
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
|
}
|
@@ -94,7 +100,6 @@ class Chan {
|
|
94
100
|
Chan.knex = knex;
|
95
101
|
}
|
96
102
|
|
97
|
-
|
98
103
|
//开始启动
|
99
104
|
beforeStart(cb) {
|
100
105
|
// 初始化一些配置
|
@@ -109,16 +114,7 @@ class Chan {
|
|
109
114
|
|
110
115
|
// 加载插件
|
111
116
|
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
|
-
}
|
117
|
+
this.loadModules('plugins');
|
122
118
|
}
|
123
119
|
|
124
120
|
/**
|
@@ -131,30 +127,22 @@ class Chan {
|
|
131
127
|
/**
|
132
128
|
* @description 模块加载入口(路由&控制器& 服务)
|
133
129
|
*/
|
134
|
-
loadModules() {
|
135
|
-
const configPath = path.join(config.APP_PATH,
|
130
|
+
loadModules(modules='modules') {
|
131
|
+
const configPath = path.join(config.APP_PATH,modules);
|
136
132
|
if (fs.existsSync(configPath)) {
|
137
133
|
const dirs = fs
|
138
134
|
.readdirSync(configPath, { withFileTypes: true })
|
139
135
|
.filter((dirent) => dirent.isDirectory())
|
140
136
|
.map((dirent) => dirent.name);
|
141
|
-
|
142
|
-
this.modulesDir = dirs;
|
137
|
+
Chan[modules]={};
|
143
138
|
for(let i=0,item;i<dirs.length;i++){
|
144
139
|
item = dirs[i];
|
145
|
-
Chan
|
140
|
+
Chan[modules][item] = {
|
146
141
|
controller: {},
|
147
142
|
service: {},
|
148
143
|
};
|
149
|
-
this.loadModule(item);
|
144
|
+
this.loadModule(modules,item);
|
150
145
|
}
|
151
|
-
|
152
|
-
//通用路由,加载错误处理和500路由和爬虫处理
|
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
|
-
}
|
158
146
|
//执行路由
|
159
147
|
// this.app.use(this.router);
|
160
148
|
}
|
@@ -165,11 +153,10 @@ class Chan {
|
|
165
153
|
* @description 加载模块,包括 controller service router
|
166
154
|
* @param {String} moduleName 模块名称
|
167
155
|
*/
|
168
|
-
loadModule(moduleName) {
|
169
|
-
|
170
|
-
this.
|
171
|
-
this.
|
172
|
-
this.loadRoutes(moduleDir,moduleName);
|
156
|
+
loadModule(modules,moduleName) {
|
157
|
+
this.loadServices(modules,moduleName);
|
158
|
+
this.loadControllers(modules,moduleName);
|
159
|
+
this.loadRoutes(modules,moduleName);
|
173
160
|
}
|
174
161
|
|
175
162
|
|
@@ -178,8 +165,8 @@ class Chan {
|
|
178
165
|
* @param {*} moduleDir 模块路径
|
179
166
|
* @param {*} moduleName 模块名称
|
180
167
|
*/
|
181
|
-
loadServices(
|
182
|
-
const servicesDir = path.join(
|
168
|
+
loadServices(modules,moduleName) {
|
169
|
+
const servicesDir = path.join(config.APP_PATH, modules, moduleName,"service");
|
183
170
|
if (fs.existsSync(servicesDir)) {
|
184
171
|
let services = fs
|
185
172
|
.readdirSync(servicesDir)
|
@@ -188,8 +175,9 @@ class Chan {
|
|
188
175
|
file= services[i]
|
189
176
|
const Service = require(path.join(servicesDir, file));
|
190
177
|
const serviceName = file.replace(".js", "");
|
191
|
-
|
192
|
-
Chan
|
178
|
+
//模块文件夹-模块文件名-服务-方法
|
179
|
+
Chan[modules][moduleName].service[serviceName] = {};
|
180
|
+
Chan[modules][moduleName].service[serviceName] = Service;
|
193
181
|
}
|
194
182
|
}
|
195
183
|
}
|
@@ -200,8 +188,8 @@ class Chan {
|
|
200
188
|
* @param {*} moduleDir 模块路径
|
201
189
|
* @param {*} moduleName 模块名称
|
202
190
|
*/
|
203
|
-
loadControllers(
|
204
|
-
const controllersDir = path.join(
|
191
|
+
loadControllers(modules,moduleName) {
|
192
|
+
const controllersDir = path.join(config.APP_PATH, modules, moduleName,"controller");
|
205
193
|
if (fs.existsSync(controllersDir)) {
|
206
194
|
let controllers = fs
|
207
195
|
.readdirSync(controllersDir)
|
@@ -210,8 +198,8 @@ class Chan {
|
|
210
198
|
file= controllers[i]
|
211
199
|
const controller = require(path.join(controllersDir, file));
|
212
200
|
const controllerName = file.replace(".js", "");
|
213
|
-
Chan
|
214
|
-
Chan
|
201
|
+
Chan[modules][moduleName].controller[controllerName] = {};
|
202
|
+
Chan[modules][moduleName].controller[controllerName] = controller;
|
215
203
|
}
|
216
204
|
}
|
217
205
|
}
|
@@ -221,19 +209,27 @@ class Chan {
|
|
221
209
|
* @param {*} moduleDir 模块路径
|
222
210
|
* @param {*} moduleName 模块名称
|
223
211
|
*/
|
224
|
-
loadRoutes(
|
225
|
-
const routersDir = path.join(
|
212
|
+
loadRoutes(modules,moduleName) {
|
213
|
+
const routersDir = path.join(config.APP_PATH, modules, moduleName,"router.js");
|
226
214
|
if (fs.existsSync(routersDir)) {
|
227
215
|
const routes = require(routersDir);
|
228
|
-
routes({router:this.router,modules:Chan
|
216
|
+
routes({router:this.router,modules:Chan[modules],app:this.app});
|
229
217
|
}
|
230
218
|
}
|
231
219
|
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
220
|
+
//通用路由,加载错误处理和500路由和爬虫处理
|
221
|
+
loadCommonRouter(){
|
222
|
+
try {
|
223
|
+
const baseRouterPath = path.join(config.APP_PATH, "router.js");
|
224
|
+
if (fs.existsSync(baseRouterPath)) {
|
225
|
+
const _router = require(baseRouterPath);
|
226
|
+
_router(this.app, this.router);
|
227
|
+
}
|
228
|
+
} catch (error) {
|
229
|
+
console.log(error);
|
230
|
+
}
|
236
231
|
}
|
232
|
+
|
237
233
|
run() {
|
238
234
|
const port = this.app.config.port || '81';
|
239
235
|
this.app.listen(port, () => {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "chanjs",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.13",
|
4
4
|
"description": "Chanjs develops a lightweight MVC framework based on express pure JavaScript",
|
5
5
|
"main": "core/chan.js",
|
6
6
|
"module":"core/chan.js",
|
@@ -16,10 +16,10 @@
|
|
16
16
|
"art-template": "^4.13.2",
|
17
17
|
"body-parser": "^1.20.2",
|
18
18
|
"cookie-parser": "^1.4.6",
|
19
|
-
"express": "^4.
|
19
|
+
"express": "^4.19.2",
|
20
20
|
"express-art-template": "^1.0.1",
|
21
|
-
"knex": "^3.0
|
21
|
+
"knex": "^3.1.0",
|
22
22
|
"morgan": "^1.10.0",
|
23
|
-
"mysql2": "^3.
|
23
|
+
"mysql2": "^3.11.0"
|
24
24
|
}
|
25
25
|
}
|