File tree Expand file tree Collapse file tree 1 file changed +85
-0
lines changed Expand file tree Collapse file tree 1 file changed +85
-0
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,91 @@ rpc.fn('a', function (a: string) {
30
30
});
31
31
```
32
32
33
+ ## 定义函数
34
+
35
+ ``` js
36
+ rpc .fn (' a' , function (a : string ) {
37
+ return a;
38
+ });
39
+ ```
40
+
41
+ ## 命名空间
42
+
43
+ ``` js
44
+ rpc .fn (' com.yourcompony.a' , function (a : string ) {
45
+ return a;
46
+ });
47
+ ```
48
+
49
+ ## Context
50
+
51
+ this === koa ctx
52
+
53
+ ``` js
54
+ rpc .fn (' com.yourcompony.a' , function (a : string ) {
55
+ return return ` ${ this .path } , ${ a} ` ;
56
+ });
57
+ ```
58
+
59
+ ## 定制
60
+
61
+ server
62
+
63
+ ``` js
64
+ rpc .fn (' com.yourcompony.a' , function (a : string ) {
65
+ if (this .method === ' post' ){
66
+ return return ` ${ this .path } , ${ a} ` ;
67
+ }
68
+ });
69
+ ```
70
+
71
+ client
72
+
73
+ ``` js
74
+ import { createClient } from ' @tomrpc/client' ;
75
+
76
+ const client = createClient ({
77
+ host: ' 127.0.0.1' ,
78
+ port: 3000 ,
79
+ namespace: ' com.yourcompony' ,
80
+ methodFilter : function (lastKey : string ) {
81
+ if (lastKey === ' a' ) {
82
+ return ' post' ;
83
+ } else {
84
+ return ' get' ;
85
+ }
86
+ },
87
+ });
88
+
89
+ const res = await client .a (' hello' );
90
+ console .dir (res);
91
+ ```
92
+
93
+ 稍后会通过com.yourcompony.getXXXX或com.yourcompony.postXXXX实现。
94
+
95
+ ## 生命周期
96
+
97
+ - beforeAll(中间件)
98
+ - beforeEach(中间件)
99
+ - beforeOne(普通函数)
100
+ - 函数执行
101
+ - afterOne(普通函数)
102
+ - afterEach(中间件)
103
+ - afterAll(中间件)
104
+
105
+
106
+ ``` js
107
+ import { createServer } from ' @tomrpc/core' ;
108
+
109
+ const rpc = createServer ({
110
+ beforeOne : function (ctx : any , key : string ) {
111
+ console .log (ctx .path );
112
+ console .log (ctx .method );
113
+ console .log (' beforeOne key=' + key);
114
+ },
115
+ });
116
+ ```
117
+
33
118
34
119
## License
35
120
You can’t perform that action at this time.
0 commit comments