topbit 3.1.3 → 3.1.4

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.
@@ -3,7 +3,7 @@
3
3
  module.exports = [
4
4
  {
5
5
  middleware: async (ctx, next) => {
6
- console.log(`global ${ctx.path} start`)
6
+ console.log(`global group:${ctx.group} path:${ctx.path} start`)
7
7
  await next(ctx)
8
8
  console.log(`global ${ctx.path} end`)
9
9
  }
@@ -11,6 +11,14 @@ class Test {
11
11
  ])
12
12
  }
13
13
 
14
+ async post(ctx) {
15
+ ctx.ok(ctx.body)
16
+ }
17
+
18
+ async list(ctx) {
19
+ ctx.ok(ctx.query)
20
+ }
21
+
14
22
  __mid() {
15
23
  return [
16
24
  {
@@ -19,7 +27,44 @@ class Test {
19
27
  await next(ctx)
20
28
  console.log(`test ${ctx.method} end`)
21
29
  }
22
- }
30
+ },
31
+
32
+ {
33
+ middleware: async (ctx, next) => {
34
+ console.log('use for get list method')
35
+ await next(ctx)
36
+ console.log('end for get list method')
37
+ },
38
+
39
+ handler: [
40
+ 'get', 'list'
41
+ ]
42
+ },
43
+
44
+ {
45
+ middleware: async (ctx, next) => {
46
+ console.log('use for get method', (new Date).toLocaleString())
47
+ await next(ctx)
48
+ console.log('end for get method', (new Date).toLocaleString())
49
+ },
50
+
51
+ handler: [
52
+ 'get'
53
+ ]
54
+ },
55
+
56
+ {
57
+ middleware: async (ctx, next) => {
58
+ console.log(' -- use for post method', (new Date).toLocaleString())
59
+ ctx.body.tag = Math.random()
60
+ await next(ctx)
61
+ console.log(' -- end for post method', (new Date).toLocaleString())
62
+ },
63
+
64
+ handler: [
65
+ 'post'
66
+ ]
67
+ },
23
68
  ]
24
69
  }
25
70
  }
package/demo/loader.js CHANGED
@@ -27,3 +27,4 @@ app.autoWorker(12)
27
27
  app.daemon(1234, 5)
28
28
 
29
29
  //console.log(app.midware.midGroup)
30
+ //console.log({...app.router})
@@ -206,10 +206,12 @@ module.exports = [
206
206
 
207
207
  ```js
208
208
  // Inside any controller file
209
- static __mid() {
209
+ __mid() {
210
210
  return [
211
211
  { name: '@vip-auth', pre: true },
212
- { name: 'log', method: 'POST' }
212
+ { name: 'log', method: 'POST' },
213
+ //use for controller method: get list
214
+ { name: 'check', handler: ['get', 'list'] }
213
215
  ]
214
216
  }
215
217
  ```
@@ -205,10 +205,13 @@ module.exports = [
205
205
 
206
206
  ```js
207
207
  // 在 controller/user.js 中
208
- static __mid() {
208
+ // 只在本文件生效
209
+ __mid() {
209
210
  return [
210
- { name: '@vip-auth', pre: true }, // 只在本文件生效
211
- { name: 'log', method: 'POST' }
211
+ { name: '@vip-auth', pre: true },
212
+ { name: 'log', method: 'POST' },
213
+ //只对控制器方法get list 启用中间件
214
+ { name: 'check', handler: ['get', 'list'] }
212
215
  ]
213
216
  }
214
217
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topbit",
3
- "version": "3.1.3",
3
+ "version": "3.1.4",
4
4
  "description": "A Server-side web framework support http/1.1 and http/2",
5
5
  "main": "src/topbit.js",
6
6
  "directories": {
@@ -56,7 +56,7 @@ class TopbitLoader {
56
56
  multi: false,
57
57
  optionsRoute: true,
58
58
  fileAsGroup: true,
59
-
59
+
60
60
  beforeController: null,
61
61
  afterController: null,
62
62
 
@@ -566,22 +566,22 @@ class TopbitLoader {
566
566
  let opts = { group };
567
567
  f = `${this.config.prePath}${f}`;
568
568
 
569
- if (!this.fileAsGroup && m.path === undefined) {
570
- m.path = [
569
+ if (!this.config.fileAsGroup && m.handler === undefined) {
570
+ m.handler = [
571
571
  'get', 'list', 'post', 'put', 'delete',
572
572
  'options', 'patch', 'head', 'trace'
573
573
  ];
574
574
  }
575
575
 
576
- if (m.path && typeof m.path === 'string') m.path = [ m.path ];
576
+ if (m.handler && typeof m.handler === 'string') m.handler = [ m.handler ];
577
577
 
578
- if (m.path && Array.isArray(m.path)) {
578
+ if (m.handler && Array.isArray(m.handler)) {
579
579
  opts.name = [];
580
- let path_num;
581
- for (let p of m.path) {
582
- path_num = this.methodNumber[p.toLowerCase()];
583
- if (path_num === undefined) continue;
584
- opts.name.push(`${f}/${path_num}`);
580
+ let handler_num;
581
+ for (let p of m.handler) {
582
+ handler_num = this.methodNumber[p.toLowerCase()];
583
+ if (handler_num === undefined) continue;
584
+ opts.name.push(`${f}/${handler_num}`);
585
585
  }
586
586
  }
587
587