chanjs 2.3.0 → 2.3.1

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.
Files changed (2) hide show
  1. package/App.js +47 -50
  2. package/package.json +1 -1
package/App.js CHANGED
@@ -85,7 +85,7 @@ class Chan {
85
85
  ...options,
86
86
  };
87
87
 
88
- this._isStarted = false;
88
+
89
89
  this.router = express.Router();
90
90
  }
91
91
 
@@ -97,51 +97,48 @@ class Chan {
97
97
  * @returns {Promise<void>}
98
98
  */
99
99
  async start() {
100
- if (this._isStarted) {
101
- console.warn("App already started");
102
- return;
103
- }
104
-
100
+
105
101
  global.appContext = this.context;
106
102
  global.Chan = Chan;
107
103
 
108
- await this.initConfig();
109
- await this.initDB();
110
- await this.initExtend();
111
- await this.initMiddleware();
112
-
113
- this._applyConfig();
114
-
104
+ //加载配置
105
+ await this.config();
106
+ //加载数据库
107
+ await this.loadDB();
108
+ //加载扩展方法
109
+ await this.loadExtend();
110
+ //加载中间件
111
+ await this.loadAppMiddleware();
112
+ //设置app
113
+ this.setApp();
114
+ //加载路由
115
115
  await this.loadRouter();
116
116
  await this.loadCommonRouter();
117
+ this.useRouter();
118
+ //404 500 处理
119
+ this.setErrorHandler();
117
120
 
118
- this._applyRoutes();
119
- this._applyErrorHandling();
120
-
121
- await this._listen();
122
-
123
- this._isStarted = true;
124
121
  }
125
122
 
126
123
  /**
127
- * 初始化配置
124
+ * 加载配置
128
125
  * @async
129
126
  * @returns {Promise<void>}
130
127
  * @description 加载应用配置并设置到上下文
131
128
  */
132
- async initConfig() {
129
+ async config() {
133
130
  let config = await loadConfig();
134
131
  Chan.config = config;
135
132
  this.context.set("config", config);
136
133
  }
137
134
 
138
135
  /**
139
- * 初始化数据库
136
+ * 加载数据库
140
137
  * @async
141
138
  * @returns {Promise<void>}
142
139
  * @description 根据配置初始化所有数据库连接
143
140
  */
144
- async initDB() {
141
+ async loadDB() {
145
142
  const dbList = Chan.config?.db || [];
146
143
  const connections = this.dbManager.getConnections();
147
144
 
@@ -168,12 +165,12 @@ class Chan {
168
165
  }
169
166
 
170
167
  /**
171
- * 初始化扩展模块
168
+ * 加载扩展模块
172
169
  * @async
173
170
  * @returns {Promise<void>}
174
171
  * @description 加载 common、helper、extend 目录下的扩展模块
175
172
  */
176
- async initExtend() {
173
+ async loadExtend() {
177
174
  const extensions = [
178
175
  { _path: Paths.commonPath, key: "common" },
179
176
  { _path: Paths.helperPath, key: "helper" },
@@ -210,9 +207,20 @@ class Chan {
210
207
  * @returns {Promise<void>}
211
208
  * @description 配置并注册所有应用中间件
212
209
  */
213
- async initMiddleware() {
210
+ async loadAppMiddleware() {
214
211
  const config = Chan.config;
215
- const { views = [], env = "development", APP_NAME = "ChanCMS", APP_VERSION = "1.0.0", cookieKey, BODY_LIMIT = "10mb", statics = [], cors = {}, PROXY = "false", waf: wafConfig = { enabled: false } } = config;
212
+ const {
213
+ views = [],
214
+ env = "development",
215
+ APP_NAME = "ChanCMS",
216
+ APP_VERSION = "1.0.0",
217
+ cookieKey,
218
+ BODY_LIMIT = "10mb",
219
+ statics = [],
220
+ cors = {},
221
+ PROXY = "false",
222
+ waf: wafConfig = { enabled: false }
223
+ } = config;
216
224
 
217
225
  await waf(this.app, wafConfig);
218
226
  setFavicon(this.app);
@@ -229,7 +237,7 @@ class Chan {
229
237
  * @private
230
238
  * @description 设置 Express 应用配置
231
239
  */
232
- _applyConfig() {
240
+ setApp() {
233
241
  this.app.set("trust proxy", Chan.config.PROXY === "true");
234
242
  this.app.set("env", this.options.env);
235
243
  this.app.disable("x-powered-by");
@@ -240,20 +248,22 @@ class Chan {
240
248
  * @private
241
249
  * @description 将所有路由应用到 Express 应用
242
250
  */
243
- _applyRoutes() {
251
+ useRouter() {
244
252
  this.app.use(this.router);
245
253
  }
246
254
 
247
255
  /**
248
- * 应用错误处理
256
+ * 应用错误处理404 500
249
257
  * @private
250
258
  * @description 配置全局错误处理中间件
251
259
  */
252
- _applyErrorHandling() {
260
+ setErrorHandler() {
261
+ //404
253
262
  this.app.use((req, res) => {
254
263
  res.status(404).json(notFoundResponse(req));
255
264
  });
256
265
 
266
+ // 500
257
267
  this.app.use((err, req, res, next) => {
258
268
  if (res.headersSent) return next(err);
259
269
 
@@ -326,23 +336,7 @@ class Chan {
326
336
  return { message: stackLines[0] || '未知错误', file: errorLine };
327
337
  }
328
338
 
329
- /**
330
- * 启动服务器监听
331
- * @async
332
- * @private
333
- * @returns {Promise<void>}
334
- * @description 启动 HTTP 服务器监听指定端口
335
- */
336
- async _listen() {
337
- return new Promise((resolve, reject) => {
338
- const server = this.app.listen(this.options.port, () => {
339
- console.log(`Server running on port ${this.options.port}`);
340
- resolve(server);
341
- });
342
-
343
- server.on("error", reject);
344
- });
345
- }
339
+
346
340
 
347
341
  /**
348
342
  * 加载模块路由
@@ -379,8 +373,11 @@ class Chan {
379
373
  * @param {Function} [cb] - 回调函数
380
374
  * @description 在启动前执行回调,传递端口号
381
375
  */
382
- ready(cb) {
383
- cb?.(this.options.port);
376
+ run(cb) {
377
+ this.app.listen(this.options.port, () => {
378
+ console.log(`Server running on port ${this.options.port}`);
379
+ cb?.(this.options.port);
380
+ });
384
381
  }
385
382
  }
386
383
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "chanjs",
4
- "version": "2.3.0",
4
+ "version": "2.3.1",
5
5
  "description": "chanjs基于express5 纯js研发的轻量级mvc框架。",
6
6
  "main": "index.js",
7
7
  "module": "index.js",