chanjs 1.0.22 → 1.0.23
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 +1 -1
- package/core/chan.js +37 -24
- package/core/lib/{core.js → index.js} +5 -5
- package/core/lib/middleware/{view.js → template.js} +2 -2
- package/core/lib/service/service.js +166 -3
- package/package.json +5 -2
- package/core/lib/base.js +0 -35
- package/core/lib/middleware/router.js +0 -31
package/README.md
CHANGED
package/core/chan.js
CHANGED
@@ -5,29 +5,30 @@ const Controller = require("./lib/controller/controller.js")
|
|
5
5
|
const Service = require("./lib/service/service.js")
|
6
6
|
const path = require("path");
|
7
7
|
const fs = require("fs");
|
8
|
-
const core = require("./lib/
|
8
|
+
const core = require("./lib/index.js");
|
9
9
|
const cors = require('cors');
|
10
10
|
|
11
11
|
/**
|
12
12
|
* @description 基于express封装的mvc框架,遵循约定优于配置原则
|
13
13
|
*/
|
14
14
|
class Chan {
|
15
|
+
static config = {};
|
16
|
+
//模块
|
17
|
+
static modules = {};
|
18
|
+
//插件
|
19
|
+
static plugins = {};
|
20
|
+
//工具
|
21
|
+
static helper = {};
|
22
|
+
static Controller = null;
|
23
|
+
static Service = null;
|
15
24
|
constructor(options={}) {
|
16
25
|
//配置
|
17
26
|
Chan.config = {...config,...options};
|
18
|
-
//模块
|
19
|
-
Chan.modules ={};
|
20
|
-
//插件
|
21
|
-
Chan.plugins ={};
|
22
|
-
//工具
|
23
|
-
Chan.helper ={};
|
24
|
-
|
25
27
|
//控制器基类
|
26
28
|
Chan.Controller = Controller;
|
27
|
-
|
28
29
|
//应用实例
|
29
30
|
this.app = express();
|
30
|
-
this.app.config = Chan.config;
|
31
|
+
// this.app.config = Chan.config;
|
31
32
|
this.router = express.Router();
|
32
33
|
//加载配置
|
33
34
|
this.loadConfig();
|
@@ -54,17 +55,18 @@ class Chan {
|
|
54
55
|
|
55
56
|
// 加载配置
|
56
57
|
loadConfig() {
|
57
|
-
const configPath = path.join(config.APP_PATH, "config/index.js");
|
58
|
+
const configPath = path.join(Chan.config.APP_PATH, "config/index.js");
|
58
59
|
if (fs.existsSync(configPath)) {
|
59
60
|
const config = require(configPath);
|
60
|
-
let allConfig = {...Chan.config,...config};
|
61
|
-
this.app.config = Chan.config = allConfig;
|
61
|
+
// let allConfig = {...Chan.config,...config};
|
62
|
+
// this.app.config = Chan.config = allConfig;
|
63
|
+
Chan.config = {...Chan.config,...config};
|
62
64
|
}
|
63
65
|
}
|
64
66
|
|
65
67
|
// 加载扩展
|
66
68
|
loadExtends() {
|
67
|
-
const extendPath = path.join(config.APP_PATH, "extend");
|
69
|
+
const extendPath = path.join(Chan.config.APP_PATH, "extend");
|
68
70
|
if (fs.existsSync(extendPath)) {
|
69
71
|
let controllers = fs
|
70
72
|
.readdirSync(extendPath)
|
@@ -82,7 +84,16 @@ class Chan {
|
|
82
84
|
//数据库操作
|
83
85
|
loadKnex(){
|
84
86
|
// 连接数据库
|
85
|
-
const {
|
87
|
+
const {
|
88
|
+
host='localhost',
|
89
|
+
port='3306',
|
90
|
+
user,
|
91
|
+
password,
|
92
|
+
database,
|
93
|
+
client='mysql2',
|
94
|
+
charset='utf8mb4'
|
95
|
+
} = Chan.config.database;
|
96
|
+
|
86
97
|
const knex = require("knex")({
|
87
98
|
client: client,
|
88
99
|
connection: {
|
@@ -93,7 +104,7 @@ class Chan {
|
|
93
104
|
database,
|
94
105
|
charset,
|
95
106
|
},
|
96
|
-
debug: config.debug, //指明是否开启debug模式,默认为true表示开启
|
107
|
+
debug: Chan.config.debug, //指明是否开启debug模式,默认为true表示开启
|
97
108
|
pool: {
|
98
109
|
//指明数据库连接池的大小,默认为{min: 2, max: 10}
|
99
110
|
min: 0,
|
@@ -108,6 +119,7 @@ class Chan {
|
|
108
119
|
},
|
109
120
|
},
|
110
121
|
});
|
122
|
+
|
111
123
|
//数据库操作实例
|
112
124
|
Chan.knex = knex;
|
113
125
|
//数据库操作基类
|
@@ -140,14 +152,15 @@ class Chan {
|
|
140
152
|
* @description app核心模块:日志、favicon 图标、cookie、json、url、模板引擎、静态资源
|
141
153
|
*/
|
142
154
|
loadCore(){
|
143
|
-
|
155
|
+
console.log()
|
156
|
+
core(this.app, Chan.config);
|
144
157
|
}
|
145
158
|
|
146
159
|
/**
|
147
160
|
* @description 模块加载入口(路由&控制器& 服务)
|
148
161
|
*/
|
149
162
|
loadModules(modules='modules') {
|
150
|
-
const configPath = path.join(config.APP_PATH,modules);
|
163
|
+
const configPath = path.join(Chan.config.APP_PATH,modules);
|
151
164
|
if (fs.existsSync(configPath)) {
|
152
165
|
const dirs = fs
|
153
166
|
.readdirSync(configPath, { withFileTypes: true })
|
@@ -185,7 +198,7 @@ class Chan {
|
|
185
198
|
* @param {*} moduleName 模块名称
|
186
199
|
*/
|
187
200
|
loadServices(modules,moduleName) {
|
188
|
-
const servicesDir = path.join(config.APP_PATH, modules, moduleName,"service");
|
201
|
+
const servicesDir = path.join(Chan.config.APP_PATH, modules, moduleName,"service");
|
189
202
|
if (fs.existsSync(servicesDir)) {
|
190
203
|
let services = fs
|
191
204
|
.readdirSync(servicesDir)
|
@@ -208,7 +221,7 @@ class Chan {
|
|
208
221
|
* @param {*} moduleName 模块名称
|
209
222
|
*/
|
210
223
|
loadControllers(modules,moduleName) {
|
211
|
-
const controllersDir = path.join(config.APP_PATH, modules, moduleName,"controller");
|
224
|
+
const controllersDir = path.join(Chan.config.APP_PATH, modules, moduleName,"controller");
|
212
225
|
if (fs.existsSync(controllersDir)) {
|
213
226
|
let controllers = fs
|
214
227
|
.readdirSync(controllersDir)
|
@@ -229,7 +242,7 @@ class Chan {
|
|
229
242
|
* @param {*} moduleName 模块名称
|
230
243
|
*/
|
231
244
|
loadRoutes(modules,moduleName) {
|
232
|
-
const routersDir = path.join(config.APP_PATH, modules, moduleName,"router.js");
|
245
|
+
const routersDir = path.join(Chan.config.APP_PATH, modules, moduleName,"router.js");
|
233
246
|
if (fs.existsSync(routersDir)) {
|
234
247
|
const routes = require(routersDir);
|
235
248
|
routes({router:this.router,modules:Chan[modules],app:this.app});
|
@@ -239,10 +252,10 @@ class Chan {
|
|
239
252
|
//通用路由,加载错误处理和500路由和爬虫处理
|
240
253
|
loadCommonRouter(){
|
241
254
|
try {
|
242
|
-
const baseRouterPath = path.join(config.APP_PATH, "router.js");
|
255
|
+
const baseRouterPath = path.join(Chan.config.APP_PATH, "router.js");
|
243
256
|
if (fs.existsSync(baseRouterPath)) {
|
244
257
|
const _router = require(baseRouterPath);
|
245
|
-
_router(this.app, this.router);
|
258
|
+
_router(this.app, this.router,Chan.config);
|
246
259
|
}
|
247
260
|
} catch (error) {
|
248
261
|
console.log(error);
|
@@ -250,7 +263,7 @@ class Chan {
|
|
250
263
|
}
|
251
264
|
|
252
265
|
run() {
|
253
|
-
const port =
|
266
|
+
const port = Chan.config.port || '81';
|
254
267
|
this.app.listen(port, () => {
|
255
268
|
console.log(`Server is running on port ${port}`);
|
256
269
|
});
|
@@ -3,10 +3,10 @@ const cookieParser = require("cookie-parser");
|
|
3
3
|
const favicon = require("serve-favicon");
|
4
4
|
const morgan = require("morgan");
|
5
5
|
const path = require("path");
|
6
|
-
const
|
7
|
-
module.exports = async function (app) {
|
6
|
+
const template = require("./middleware/template.js");
|
7
|
+
module.exports = async function (app,config) {
|
8
8
|
const { logger, APP_PATH, cookieKey, maxAge, JSON_LIMIT, appName, version } =
|
9
|
-
|
9
|
+
config;
|
10
10
|
//日志
|
11
11
|
app.use(morgan(logger.level));
|
12
12
|
|
@@ -21,7 +21,7 @@ module.exports = async function (app) {
|
|
21
21
|
app.use(express.urlencoded({ extended: false }));
|
22
22
|
|
23
23
|
//配置模板引擎
|
24
|
-
|
24
|
+
template(app,config);
|
25
25
|
|
26
26
|
//使用静态资源 ,
|
27
27
|
app.use(
|
@@ -33,7 +33,7 @@ module.exports = async function (app) {
|
|
33
33
|
|
34
34
|
//设置头信息
|
35
35
|
app.use((req, res, next) => {
|
36
|
-
res.setHeader("
|
36
|
+
res.setHeader("Create-By", "Chanjs");
|
37
37
|
res.setHeader("X-Powered-By", appName);
|
38
38
|
res.setHeader(appName, version);
|
39
39
|
next();
|
@@ -1,7 +1,7 @@
|
|
1
1
|
const path = require("path");
|
2
|
-
module.exports = (app) => {
|
2
|
+
module.exports = (app,config) => {
|
3
3
|
const
|
4
|
-
{ APP_PATH, views,env } =
|
4
|
+
{ APP_PATH, views,env } = config;
|
5
5
|
//默认home view
|
6
6
|
const home = path.join(APP_PATH, `modules/web/view`);
|
7
7
|
//合并插件中的view
|
@@ -1,28 +1,191 @@
|
|
1
1
|
class BaseService {
|
2
|
+
/**
|
3
|
+
* @description 构造函数
|
4
|
+
* @param {*} knex - knex实例
|
5
|
+
* @param {*} model - 模型名称
|
6
|
+
*/
|
2
7
|
constructor(knex, model='') {
|
3
8
|
this.knex = knex;
|
4
9
|
this.model = model;
|
5
10
|
}
|
6
11
|
|
12
|
+
/**
|
13
|
+
* @description 查找所有符合条件的记录
|
14
|
+
* @param {*} query - 查询条件,例如:{name: 'test', age: 18}
|
15
|
+
* @returns {Promise} 返回符合条件的记录
|
16
|
+
*/
|
17
|
+
query(query) {
|
18
|
+
return this.knex(this.model).where(query);
|
19
|
+
}
|
20
|
+
|
21
|
+
/**
|
22
|
+
* @description 查询表所有记录,慎用
|
23
|
+
* @returns {Promise} 返回所有记录
|
24
|
+
*/
|
7
25
|
all() {
|
8
26
|
return this.knex(this.model).select();
|
9
27
|
}
|
10
28
|
|
11
|
-
|
29
|
+
/**
|
30
|
+
* @description 查询指定id的记录
|
31
|
+
* @param {*} id
|
32
|
+
* @returns {Promise} 返回指定id的记录
|
33
|
+
*/
|
34
|
+
findById(id) {
|
35
|
+
return this.knex(this.model).where('id', id).first();
|
36
|
+
}
|
37
|
+
|
38
|
+
/**
|
39
|
+
* @description 查询分页数据
|
40
|
+
* @param {*} current
|
41
|
+
* @param {*} pageSize
|
42
|
+
* @param {*} query
|
43
|
+
* @returns {Promise} 返回分页数据
|
44
|
+
*/
|
45
|
+
async findListAndQuery(current = 1, pageSize = 10, query = {}) {
|
46
|
+
const offset = (current - 1) * pageSize;
|
47
|
+
|
48
|
+
let countQuery = this.knex(this.model).count('* as total');
|
49
|
+
let dataQuery = this.knex(this.model);
|
50
|
+
|
51
|
+
// 应用查询条件
|
52
|
+
for (const key in query) {
|
53
|
+
dataQuery = dataQuery.where(key, query[key]);
|
54
|
+
countQuery = countQuery.where(key, query[key]);
|
55
|
+
}
|
56
|
+
|
57
|
+
// 获取总记录数
|
58
|
+
const total = await countQuery.first();
|
59
|
+
|
60
|
+
// 获取分页数据
|
61
|
+
const data = await dataQuery.offset(offset).limit(pageSize);
|
62
|
+
|
63
|
+
return { data, total: parseInt(total.total, 10) };
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* @description 查询指定id的记录
|
68
|
+
* @param {*} id - 记录id
|
69
|
+
* @returns {Promise} 返回指定id的记录
|
70
|
+
*/
|
71
|
+
findOneById(id) {
|
12
72
|
return this.knex(this.model).where('id', id).select();
|
13
73
|
}
|
14
74
|
|
75
|
+
/**
|
76
|
+
* @description 查询指定条件的记录
|
77
|
+
* @param {Object} query - {<key>:<value>}
|
78
|
+
* @returns {Promise} 返回指定条件的记录
|
79
|
+
*/
|
80
|
+
findOne(query) {
|
81
|
+
return this.knex(this.model).where(query).first();
|
82
|
+
}
|
83
|
+
|
84
|
+
/**
|
85
|
+
* @description 更新指定id的记录
|
86
|
+
* @param {*} id - 记录id
|
87
|
+
* @param {*} params - {<key>:<value>}
|
88
|
+
* @returns {Promise} 返回更新后的记录
|
89
|
+
*/
|
90
|
+
findOneAndUpdate(id, params) {
|
91
|
+
return this.knex(this.model).where('id', id).update(params).returning('*');
|
92
|
+
}
|
93
|
+
|
94
|
+
|
95
|
+
/**
|
96
|
+
* @description 查询指定条件的记录数量
|
97
|
+
* @param {*} query - {<key>:<value>}
|
98
|
+
* @returns {Promise} 返回指定条件的记录数量
|
99
|
+
*/
|
100
|
+
count(query) {
|
101
|
+
return this.knex(this.model).where(query).count('* as total').first();
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* @description 插入一条记录
|
106
|
+
* @param {*} params - {<key>:<value>}
|
107
|
+
* @returns {Promise} 返回插入后的记录
|
108
|
+
*/
|
15
109
|
insert(params) {
|
16
110
|
return this.knex(this.model).insert(params);
|
17
111
|
}
|
18
112
|
|
19
|
-
|
113
|
+
/**
|
114
|
+
* @description 插入多条记录
|
115
|
+
* @param {*} records - [{<key>:<value>}, {<key>:<value>}, ...]
|
116
|
+
* @returns {Promise} 返回插入后的记录
|
117
|
+
*/
|
118
|
+
insertMany(records) {
|
119
|
+
return this.knex(this.model).insert(records);
|
120
|
+
}
|
121
|
+
|
122
|
+
/**
|
123
|
+
* @description 更新指定id的记录
|
124
|
+
* @param {*} id - 记录id
|
125
|
+
* @param {*} params - {<key>:<value>}
|
126
|
+
* @returns {Promise} 返回更新后的记录
|
127
|
+
*/
|
128
|
+
updateById(id, params) {
|
20
129
|
return this.knex(this.model).where('id', id).update(params);
|
21
130
|
}
|
22
131
|
|
23
|
-
|
132
|
+
/**
|
133
|
+
* @description 更新数据
|
134
|
+
* @param {*} query - {<key>:<value>}
|
135
|
+
* @param {*} update - {<key>:<value>}
|
136
|
+
* @returns {Promise<*>} - 返回更新后的数据
|
137
|
+
*/
|
138
|
+
update(query, update) {
|
139
|
+
return this.knex(this.model).where(query).update(update);
|
140
|
+
}
|
141
|
+
|
142
|
+
/**
|
143
|
+
* @description 查询并更新
|
144
|
+
* @param {*} query - {<key>:<value>}
|
145
|
+
* @param {*} update - {<key>:<value>}
|
146
|
+
* @returns {Promise<*>} - 返回更新后的记录
|
147
|
+
*/
|
148
|
+
findAndModify(query, update) {
|
149
|
+
return this.knex(this.model).where(query).update(update);
|
150
|
+
}
|
151
|
+
|
152
|
+
/**
|
153
|
+
*
|
154
|
+
* @param {*} id - id
|
155
|
+
* @param {*} update - {<key>:<value>}
|
156
|
+
* @returns {Promise<*>} - 返回更新后的记录
|
157
|
+
*/
|
158
|
+
findByIdAndUpdate(id, update) {
|
159
|
+
return this.knex(this.model).where('id', id).update(update);
|
160
|
+
}
|
161
|
+
|
162
|
+
/**
|
163
|
+
* @description 根据条件更新记录
|
164
|
+
* @param {*} query - {<key>:<value>}
|
165
|
+
* @param {*} update - {<key>:<value>}
|
166
|
+
* @returns {Promise} - 返回更新后的记录
|
167
|
+
*/
|
168
|
+
findOneAndUpdate(query, update) {
|
169
|
+
return this.knex(this.model).where(query).update(update);
|
170
|
+
}
|
171
|
+
|
172
|
+
/**
|
173
|
+
* @description 根据id删除一条记录
|
174
|
+
* @param {*} id - 记录id
|
175
|
+
* @returns {Promise} - 返回删除的记录
|
176
|
+
*/
|
177
|
+
findByIdAndRemove(id) {
|
24
178
|
return this.knex(this.model).where('id', id).del();
|
25
179
|
}
|
180
|
+
|
181
|
+
/**
|
182
|
+
* @description 根据条件删除一条记录
|
183
|
+
* @param {*} query
|
184
|
+
* @returns {Promise} - 返回删除的记录
|
185
|
+
*/
|
186
|
+
findOneAndRemove(query) {
|
187
|
+
return this.knex(this.model).where(query).del();
|
188
|
+
}
|
26
189
|
}
|
27
190
|
|
28
191
|
const Service = (knex, model='') => {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "chanjs",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.23",
|
4
4
|
"description": "Chan.js基于express 纯js研发的轻量级mvc框架。",
|
5
5
|
"main": "core/chan.js",
|
6
6
|
"module": "core/chan.js",
|
@@ -22,5 +22,8 @@
|
|
22
22
|
"knex": "^3.1.0",
|
23
23
|
"morgan": "^1.10.0",
|
24
24
|
"mysql2": "^3.11.0"
|
25
|
-
}
|
25
|
+
},
|
26
|
+
"__npminstall_done": true,
|
27
|
+
"_from": "chanjs@1.0.22",
|
28
|
+
"_resolved": "https://registry.npmmirror.com/chanjs/-/chanjs-1.0.22.tgz"
|
26
29
|
}
|
package/core/lib/base.js
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
const express = require("express");
|
2
|
-
const cookieParser = require("cookie-parser");
|
3
|
-
const favicon = require("serve-favicon");
|
4
|
-
const morgan = require("morgan");
|
5
|
-
const path = require("path");
|
6
|
-
|
7
|
-
const view = require("./middleware/view.js");
|
8
|
-
module.exports = async function (app) {
|
9
|
-
|
10
|
-
const { logger,APP_PATH, cookieKey, maxAge } = app.config;
|
11
|
-
//日志
|
12
|
-
app.use(morgan(logger.level));
|
13
|
-
|
14
|
-
// favicon 图标
|
15
|
-
app.use(favicon(path.join(APP_PATH, "public/favicon.ico")));
|
16
|
-
|
17
|
-
//cookie
|
18
|
-
app.use(cookieParser(cookieKey));
|
19
|
-
|
20
|
-
//解析接口 json & url
|
21
|
-
app.use(express.json());
|
22
|
-
app.use(express.urlencoded({ extended: false }));
|
23
|
-
|
24
|
-
//配置模板引擎
|
25
|
-
view(app);
|
26
|
-
|
27
|
-
//使用静态资源 ,
|
28
|
-
app.use(
|
29
|
-
"/public",
|
30
|
-
express.static(path.join(APP_PATH, "public"), {
|
31
|
-
maxAge:maxAge,
|
32
|
-
})
|
33
|
-
);
|
34
|
-
|
35
|
-
};
|
@@ -1,31 +0,0 @@
|
|
1
|
-
const path = require("path");
|
2
|
-
const dealRouter = (app,router) => {
|
3
|
-
|
4
|
-
const {config:{template='default',APP_PATH}} = app;
|
5
|
-
//机器人抓取
|
6
|
-
router.get("/robots.txt", function (req, res, next) {
|
7
|
-
res.type('text/plain');
|
8
|
-
res.sendFile(path.join(APP_PATH, "/public/robots.txt"));
|
9
|
-
});
|
10
|
-
|
11
|
-
//404处理
|
12
|
-
router.use((req, res, next) => {
|
13
|
-
let ip = req.headers["x-forwarded-for"] || req.ip;
|
14
|
-
console.log("404-->", `${req.method}-${decodeURI(req.url)}-${ip}`);
|
15
|
-
res.render(`${template}/404.html`);
|
16
|
-
});
|
17
|
-
|
18
|
-
//处理错误
|
19
|
-
router.use((err, req, res) => {
|
20
|
-
console.error("500-->", req.method, req.url, err);
|
21
|
-
let data = { url: req.url, method: req.method, error: err.message };
|
22
|
-
if (req.is("html") || req.is("html") == null) {
|
23
|
-
res.render(`${template}/500.html`, { data });
|
24
|
-
} else {
|
25
|
-
res.json({ code: 500, data, msg: data.error });
|
26
|
-
}
|
27
|
-
});
|
28
|
-
|
29
|
-
};
|
30
|
-
|
31
|
-
module.exports = dealRouter;
|