@fastcar/koa 0.1.20 → 0.1.22
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 +398 -69
- package/package.json +28 -19
- package/src/KoaApplication.ts +2 -2
- package/target/KoaApplication.js +2 -2
- package/test/logs/sys.log +23 -0
package/README.md
CHANGED
|
@@ -1,115 +1,444 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @fastcar/koa
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
基于 [@fastcar/core](https://github.com/williamDazhangyu/fast-car) 框架的 Koa Web 服务封装组件,提供装饰器驱动的路由定义和中间件管理。
|
|
4
4
|
|
|
5
|
+
## 目录
|
|
6
|
+
|
|
7
|
+
- [特性](#特性)
|
|
8
|
+
- [安装](#安装)
|
|
9
|
+
- [快速开始](#快速开始)
|
|
10
|
+
- [配置](#配置)
|
|
11
|
+
- [装饰器/API 参考](#装饰器api-参考)
|
|
12
|
+
- [内置中间件](#内置中间件)
|
|
13
|
+
- [示例](#示例)
|
|
14
|
+
- [类型定义](#类型定义)
|
|
15
|
+
- [依赖关系](#依赖关系)
|
|
16
|
+
- [许可证](#许可证)
|
|
17
|
+
|
|
18
|
+
## 特性
|
|
19
|
+
|
|
20
|
+
- 🎯 **装饰器驱动**: 使用 TypeScript 装饰器定义路由和中间件
|
|
21
|
+
- 🔌 **模块化中间件**: 支持自定义和内置中间件,按声明顺序加载
|
|
22
|
+
- 🚀 **自动依赖注入**: 集成 @fastcar/core 的 IoC 容器
|
|
23
|
+
- 📁 **静态文件服务**: 内置静态文件和文件上传支持
|
|
24
|
+
- 🛡️ **全局异常处理**: 内置异常捕获中间件
|
|
25
|
+
- 📚 **Swagger 文档**: 支持 API 文档自动生成(需安装 swagger-ui-dist)
|
|
26
|
+
- 🔄 **代理支持**: 内置反向代理中间件
|
|
27
|
+
|
|
28
|
+
## 安装
|
|
29
|
+
|
|
30
|
+
```bash
|
|
5
31
|
npm install @fastcar/koa
|
|
32
|
+
# 或
|
|
33
|
+
yarn add @fastcar/koa
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 必需依赖
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm install @fastcar/core @fastcar/server koa @koa/router
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 可选依赖
|
|
43
|
+
|
|
44
|
+
根据功能需要安装:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# 文件上传
|
|
48
|
+
npm install @koa/multer multer
|
|
49
|
+
|
|
50
|
+
# 静态文件服务
|
|
51
|
+
npm install koa-static koa-range koa-mount
|
|
52
|
+
|
|
53
|
+
# 跨域支持
|
|
54
|
+
npm install koa2-cors
|
|
6
55
|
|
|
7
|
-
|
|
56
|
+
# 请求体解析
|
|
57
|
+
npm install @koa/bodyparser koa-body
|
|
8
58
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* 停止阶段 延迟一秒左右 将server进行关闭
|
|
59
|
+
# 反向代理
|
|
60
|
+
npm install http-proxy-middleware koa2-connect
|
|
12
61
|
|
|
13
|
-
|
|
62
|
+
# API 文档
|
|
63
|
+
npm install swagger-ui-dist
|
|
14
64
|
|
|
15
|
-
|
|
65
|
+
# 类型定义(TypeScript 项目)
|
|
66
|
+
npm install -D @types/koa @types/koa__multer @types/koa-mount @types/koa-range @types/koa-static @types/koa2-cors
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## 快速开始
|
|
70
|
+
|
|
71
|
+
### 1. 创建应用入口
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
16
74
|
import { FastCarApplication } from "@fastcar/core";
|
|
17
75
|
import { Application } from "@fastcar/core/annotation";
|
|
18
|
-
import { EnableKoa } from "@fastcar/koa/annotation";
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
@
|
|
22
|
-
|
|
23
|
-
|
|
76
|
+
import { EnableKoa, KoaMiddleware } from "@fastcar/koa/annotation";
|
|
77
|
+
import { ExceptionGlobalHandler, KoaBodyParser } from "@fastcar/koa";
|
|
78
|
+
|
|
79
|
+
@Application
|
|
80
|
+
@EnableKoa
|
|
81
|
+
@KoaMiddleware(ExceptionGlobalHandler, KoaBodyParser)
|
|
82
|
+
class App {
|
|
83
|
+
app!: FastCarApplication;
|
|
24
84
|
}
|
|
25
85
|
|
|
26
|
-
export const app = new
|
|
86
|
+
export const app = new App();
|
|
27
87
|
```
|
|
28
88
|
|
|
29
|
-
|
|
89
|
+
### 2. 创建控制器
|
|
30
90
|
|
|
31
|
-
```
|
|
91
|
+
```typescript
|
|
32
92
|
import { Controller } from "@fastcar/core/annotation";
|
|
33
|
-
import { GET } from "@fastcar/koa/annotation";
|
|
93
|
+
import { GET, POST, RequestMapping } from "@fastcar/koa/annotation";
|
|
34
94
|
import { Context } from "koa";
|
|
35
95
|
|
|
36
|
-
|
|
37
96
|
@Controller
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
97
|
+
@RequestMapping("/api") // 基础路径前缀
|
|
98
|
+
export default class UserController {
|
|
99
|
+
|
|
100
|
+
@GET("/users")
|
|
101
|
+
async listUsers() {
|
|
102
|
+
return { code: 200, data: [] };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
@POST("/users")
|
|
106
|
+
async createUser(data: any, ctx: Context) {
|
|
107
|
+
// data 自动合并了 query、body 和 params
|
|
108
|
+
return { code: 200, data: { id: 1, ...data } };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
@GET("/users/:id")
|
|
112
|
+
async getUser(data: { id: string }, ctx: Context) {
|
|
113
|
+
// URL 参数在 ctx.params 中
|
|
114
|
+
const userId = ctx.params.id;
|
|
115
|
+
return { code: 200, data: { id: userId } };
|
|
116
|
+
}
|
|
47
117
|
}
|
|
48
118
|
```
|
|
49
119
|
|
|
50
|
-
|
|
120
|
+
### 3. 配置文件 (application.yaml)
|
|
121
|
+
|
|
122
|
+
```yaml
|
|
123
|
+
koa:
|
|
124
|
+
server:
|
|
125
|
+
port: 3000
|
|
126
|
+
hostname: "0.0.0.0"
|
|
127
|
+
koaStatic:
|
|
128
|
+
"/static": "./resource/static" # 路径别名映射
|
|
129
|
+
koaBodyOptions:
|
|
130
|
+
multipart: true
|
|
131
|
+
formidable:
|
|
132
|
+
maxFileSize: 200 * 1024 * 1024 # 200MB
|
|
133
|
+
```
|
|
51
134
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
135
|
+
## 配置
|
|
136
|
+
|
|
137
|
+
### KoaConfig 类型
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
type KoaConfig = {
|
|
141
|
+
// 服务器配置,支持多端口监听
|
|
142
|
+
server: ServerConfig | ServerConfig[];
|
|
143
|
+
|
|
144
|
+
// 静态文件路径映射 { 访问路径: 文件系统路径 }
|
|
145
|
+
koaStatic?: { [key: string]: string };
|
|
146
|
+
|
|
147
|
+
// koa-body 配置(文件上传)
|
|
148
|
+
koaBodyOptions?: { [key: string]: any };
|
|
149
|
+
|
|
150
|
+
// @koa/bodyparser 配置
|
|
151
|
+
koaBodyParser?: { [key: string]: any };
|
|
152
|
+
|
|
153
|
+
// 反向代理配置
|
|
154
|
+
koaProxy?: {
|
|
155
|
+
[path: string]: {
|
|
156
|
+
target: string;
|
|
157
|
+
changeOrigin?: boolean;
|
|
158
|
+
pathRewrite?: { [pattern: string]: string };
|
|
159
|
+
ws?: boolean;
|
|
60
160
|
};
|
|
61
|
-
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// 其他扩展配置
|
|
164
|
+
extra?: { [key: string]: any };
|
|
165
|
+
};
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## 装饰器/API 参考
|
|
169
|
+
|
|
170
|
+
### 应用级装饰器
|
|
171
|
+
|
|
172
|
+
| 装饰器 | 用途 | 参数 |
|
|
173
|
+
|--------|------|------|
|
|
174
|
+
| `@EnableKoa` | 启用 Koa 组件 | 无 |
|
|
175
|
+
| `@KoaMiddleware(...middlewares)` | 注册中间件 | `...MiddleWareType[]` - 中间件函数数组,越靠前优先级越高 |
|
|
176
|
+
|
|
177
|
+
### 路由装饰器
|
|
178
|
+
|
|
179
|
+
| 装饰器 | HTTP 方法 | 简写别名 | 参数 |
|
|
180
|
+
|--------|-----------|----------|------|
|
|
181
|
+
| `@GetMapping(path)` | GET | `@GET` | `string` - 路由路径 |
|
|
182
|
+
| `@PostMapping(path)` | POST | `@POST` | `string` - 路由路径 |
|
|
183
|
+
| `@PutMapping(path)` | PUT | `@PUT` | `string` - 路由路径 |
|
|
184
|
+
| `@DeleteMapping(path)` | DELETE | `@DELETE` | `string` - 路由路径 |
|
|
185
|
+
| `@PatchMapping(path)` | PATCH | `@PATCH` | `string` - 路由路径 |
|
|
186
|
+
| `@AllMapping(path)` | ALL | `@ALL` | `string` - 路由路径 |
|
|
187
|
+
| `@RequestMapping(path)` | - | `@REQUEST` | `string` - 基础路径前缀 |
|
|
188
|
+
|
|
189
|
+
### 控制器装饰器
|
|
190
|
+
|
|
191
|
+
| 装饰器 | 用途 |
|
|
192
|
+
|--------|------|
|
|
193
|
+
| `@Controller` | 标记类为控制器,自动扫描路由 |
|
|
194
|
+
|
|
195
|
+
### 路由方法参数
|
|
196
|
+
|
|
197
|
+
控制器方法的参数约定:
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
methodName(data: any, ctx: Context): any
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
- `data`: 自动合并 `query` + `body` + `params` 的对象(重名时 body 优先级最高)
|
|
204
|
+
- `ctx`: Koa 的 Context 对象,可访问 `ctx.params`, `ctx.request.body`, `ctx.query` 等
|
|
205
|
+
- 返回值: 自动设置为 `ctx.body`
|
|
206
|
+
|
|
207
|
+
## 内置中间件
|
|
208
|
+
|
|
209
|
+
### 1. ExceptionGlobalHandler
|
|
210
|
+
|
|
211
|
+
全局异常捕获,统一处理控制器抛出的错误。
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
import { ExceptionGlobalHandler } from "@fastcar/koa";
|
|
215
|
+
|
|
216
|
+
@KoaMiddleware(ExceptionGlobalHandler)
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### 2. KoaBodyParser
|
|
220
|
+
|
|
221
|
+
请求体解析(推荐),支持 JSON、表单等。
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
import { KoaBodyParser } from "@fastcar/koa";
|
|
225
|
+
|
|
226
|
+
@KoaMiddleware(KoaBodyParser)
|
|
227
|
+
// 配合配置
|
|
228
|
+
// koaBodyParser:
|
|
229
|
+
// jsonLimit: "1mb"
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### 3. KoaBody
|
|
233
|
+
|
|
234
|
+
文件上传解析(基于 koa-body)。
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
import { KoaBody } from "@fastcar/koa";
|
|
238
|
+
|
|
239
|
+
@KoaMiddleware(KoaBody)
|
|
240
|
+
// 配合配置
|
|
241
|
+
// koaBodyOptions:
|
|
242
|
+
// multipart: true
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### 4. KoaCors
|
|
246
|
+
|
|
247
|
+
跨域支持。
|
|
62
248
|
|
|
63
|
-
|
|
249
|
+
```typescript
|
|
250
|
+
import { KoaCors } from "@fastcar/koa";
|
|
251
|
+
|
|
252
|
+
@KoaMiddleware(KoaCors)
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### 5. KoaStatic
|
|
256
|
+
|
|
257
|
+
静态文件服务(整合 koa-static + koa-range + koa-mount)。
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
import { KoaStatic } from "@fastcar/koa";
|
|
261
|
+
|
|
262
|
+
@KoaMiddleware(KoaStatic)
|
|
263
|
+
// 配合配置
|
|
264
|
+
// koaStatic:
|
|
265
|
+
// "/": "./resource/public"
|
|
266
|
+
// "/uploads": "./uploads"
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### 6. KoaMulter
|
|
270
|
+
|
|
271
|
+
增强的文件上传解析(基于 @koa/multer)。
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
import { KoaMulter } from "@fastcar/koa";
|
|
275
|
+
|
|
276
|
+
@KoaMiddleware(KoaMulter)
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### 7. KoaProxy
|
|
280
|
+
|
|
281
|
+
反向代理(基于 http-proxy-middleware)。
|
|
282
|
+
|
|
283
|
+
```typescript
|
|
284
|
+
import { KoaProxy } from "@fastcar/koa";
|
|
285
|
+
|
|
286
|
+
@KoaMiddleware(KoaProxy)
|
|
287
|
+
// 配合配置
|
|
288
|
+
// koaProxy:
|
|
289
|
+
// "/api":
|
|
290
|
+
// target: "http://backend-server:8080"
|
|
291
|
+
// changeOrigin: true
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### 8. Swagger
|
|
295
|
+
|
|
296
|
+
API 文档服务(需安装 swagger-ui-dist)。
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
import { Swagger } from "@fastcar/koa";
|
|
300
|
+
|
|
301
|
+
@KoaMiddleware(Swagger)
|
|
302
|
+
// 访问 /swagger 查看文档
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### 9. HeaderCoding
|
|
306
|
+
|
|
307
|
+
Header 编码校验。
|
|
308
|
+
|
|
309
|
+
```typescript
|
|
310
|
+
import { HeaderCoding } from "@fastcar/koa";
|
|
311
|
+
|
|
312
|
+
@KoaMiddleware(HeaderCoding)
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
## 示例
|
|
316
|
+
|
|
317
|
+
### 自定义中间件
|
|
318
|
+
|
|
319
|
+
```typescript
|
|
320
|
+
import * as Koa from "koa";
|
|
64
321
|
import { FastCarApplication } from "@fastcar/core";
|
|
65
|
-
import {
|
|
66
|
-
|
|
322
|
+
import { KoaMiddleware } from "@fastcar/koa/annotation";
|
|
323
|
+
|
|
324
|
+
// 中间件工厂函数,接收 FastCarApplication 实例
|
|
325
|
+
function LoggerMiddleware(app: FastCarApplication): Koa.Middleware {
|
|
326
|
+
return async (ctx: Koa.Context, next: Function) => {
|
|
327
|
+
const start = Date.now();
|
|
328
|
+
console.log(`--> ${ctx.method} ${ctx.url}`);
|
|
329
|
+
|
|
330
|
+
await next();
|
|
331
|
+
|
|
332
|
+
const duration = Date.now() - start;
|
|
333
|
+
console.log(`<-- ${ctx.method} ${ctx.url} ${ctx.status} ${duration}ms`);
|
|
334
|
+
};
|
|
335
|
+
}
|
|
67
336
|
|
|
68
|
-
@Application
|
|
69
|
-
@EnableKoa
|
|
70
|
-
@KoaMiddleware(
|
|
71
|
-
class
|
|
72
|
-
|
|
337
|
+
@Application
|
|
338
|
+
@EnableKoa
|
|
339
|
+
@KoaMiddleware(LoggerMiddleware, ExceptionGlobalHandler, KoaBodyParser)
|
|
340
|
+
class App {
|
|
341
|
+
app!: FastCarApplication;
|
|
73
342
|
}
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### 多 HTTP 方法绑定
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
import { Controller } from "@fastcar/core/annotation";
|
|
349
|
+
import { GetMapping, PostMapping } from "@fastcar/koa/annotation";
|
|
350
|
+
|
|
351
|
+
@Controller
|
|
352
|
+
export default class ResourceController {
|
|
353
|
+
|
|
354
|
+
// 同一个方法支持多种 HTTP 方法
|
|
355
|
+
@GetMapping("/resource")
|
|
356
|
+
@PostMapping("/resource")
|
|
357
|
+
handleResource(data: any) {
|
|
358
|
+
return { method: 'GET or POST', data };
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
```
|
|
74
362
|
|
|
75
|
-
|
|
363
|
+
### 文件上传
|
|
364
|
+
|
|
365
|
+
```typescript
|
|
366
|
+
import { Controller } from "@fastcar/core/annotation";
|
|
367
|
+
import { POST } from "@fastcar/koa/annotation";
|
|
368
|
+
import { Context } from "koa";
|
|
369
|
+
|
|
370
|
+
@Controller
|
|
371
|
+
export default class UploadController {
|
|
372
|
+
|
|
373
|
+
@POST("/upload")
|
|
374
|
+
async uploadFile(data: any, ctx: Context) {
|
|
375
|
+
// 文件信息在 ctx.request.files 中
|
|
376
|
+
const files = ctx.request.files;
|
|
377
|
+
return {
|
|
378
|
+
code: 200,
|
|
379
|
+
message: "上传成功",
|
|
380
|
+
files: Object.keys(files || {})
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
}
|
|
76
384
|
```
|
|
77
385
|
|
|
78
|
-
##
|
|
386
|
+
## 类型定义
|
|
79
387
|
|
|
80
|
-
|
|
81
|
-
* KoaBody 用于文件上传 与 koa-body的用法一致
|
|
82
|
-
* KoaBodyParser 用于请求数据的解析 推荐客户端使用application/json的方式
|
|
83
|
-
* KoaCors 跨域设置(后期可能会用更好的插件替代)
|
|
84
|
-
* KoaStatic 整合了koa-static,koa-range,koa-mount用于静态文件访问,可设置别名
|
|
85
|
-
* Swagger 用于展示api文档使用(后期支持自动化配置说明)
|
|
388
|
+
### 导出路径
|
|
86
389
|
|
|
87
|
-
|
|
390
|
+
```typescript
|
|
391
|
+
// 主模块
|
|
392
|
+
import { KoaApplication, KoaConfig } from "@fastcar/koa";
|
|
88
393
|
|
|
89
|
-
|
|
394
|
+
// 装饰器
|
|
395
|
+
import { EnableKoa, KoaMiddleware, GET, POST, ... } from "@fastcar/koa/annotation";
|
|
396
|
+
```
|
|
90
397
|
|
|
91
|
-
|
|
398
|
+
### KoaApplication 类
|
|
92
399
|
|
|
93
|
-
|
|
400
|
+
```typescript
|
|
401
|
+
class KoaApplication {
|
|
402
|
+
public koaApp: Koa; // Koa 实例
|
|
403
|
+
|
|
404
|
+
start(): void; // 启动服务
|
|
405
|
+
stop(): Promise<void>; // 停止服务
|
|
406
|
+
}
|
|
407
|
+
```
|
|
94
408
|
|
|
95
|
-
|
|
409
|
+
### 中间件类型
|
|
96
410
|
|
|
97
|
-
|
|
411
|
+
```typescript
|
|
412
|
+
type MiddleWareType = (
|
|
413
|
+
app: FastCarApplication,
|
|
414
|
+
koaApp?: Koa
|
|
415
|
+
) => Koa.Middleware | Koa.Middleware[] | Promise<Koa.Middleware | Koa.Middleware[]>;
|
|
416
|
+
```
|
|
98
417
|
|
|
99
|
-
|
|
418
|
+
## 依赖关系
|
|
100
419
|
|
|
101
|
-
|
|
420
|
+
```
|
|
421
|
+
@fastcar/koa
|
|
422
|
+
├── @fastcar/core (peer) - IoC 容器和组件生命周期
|
|
423
|
+
├── @fastcar/server (peer) - HTTP 服务器管理
|
|
424
|
+
├── koa (^3.1.1) - Web 框架
|
|
425
|
+
└── @koa/router (^15.1.1) - 路由
|
|
426
|
+
```
|
|
102
427
|
|
|
103
|
-
|
|
428
|
+
## 生命周期
|
|
104
429
|
|
|
105
|
-
|
|
430
|
+
1. **启动阶段**(按优先级):
|
|
431
|
+
- 加载自定义中间件(按 `@KoaMiddleware` 声明顺序)
|
|
432
|
+
- 加载路由(扫描所有 `@Controller` 类)
|
|
433
|
+
- 启动 HTTP 服务监听
|
|
106
434
|
|
|
107
|
-
|
|
435
|
+
2. **停止阶段**:
|
|
436
|
+
- 延迟约 1 秒后关闭 server 连接
|
|
108
437
|
|
|
109
|
-
|
|
438
|
+
## 许可证
|
|
110
439
|
|
|
111
|
-
|
|
440
|
+
MIT License
|
|
112
441
|
|
|
113
|
-
|
|
442
|
+
---
|
|
114
443
|
|
|
115
|
-
|
|
444
|
+
**项目地址**: [https://github.com/williamDazhangyu/fast-car](https://github.com/williamDazhangyu/fast-car)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastcar/koa",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.22",
|
|
4
4
|
"homepage": "https://github.com/williamDazhangyu/fast-car",
|
|
5
5
|
"description": "fastcar框架下对koa的包装",
|
|
6
6
|
"main": "target/index.js",
|
|
@@ -33,33 +33,33 @@
|
|
|
33
33
|
"@fastcar/core": "^0.3.10",
|
|
34
34
|
"@fastcar/server": "^0.0.3",
|
|
35
35
|
"@types/co-body": "^6.1.3",
|
|
36
|
-
"@types/koa": "^
|
|
36
|
+
"@types/koa": "^3.0.1",
|
|
37
37
|
"@types/koa__multer": "*",
|
|
38
|
-
"
|
|
38
|
+
"koa2-cors": "^2.0.6",
|
|
39
39
|
"reflect-metadata": "*"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@koa/bodyparser": "^
|
|
43
|
-
"@koa/router": "^
|
|
44
|
-
"koa": "^
|
|
42
|
+
"@koa/bodyparser": "^6.0.0",
|
|
43
|
+
"@koa/router": "^15.1.1",
|
|
44
|
+
"koa": "^3.1.1"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"@fastcar/core": "*",
|
|
48
48
|
"@fastcar/server": "*",
|
|
49
|
-
"@koa/multer": "
|
|
50
|
-
"@types/koa-mount": "
|
|
51
|
-
"@types/koa-range": "
|
|
52
|
-
"@types/koa-static": "
|
|
53
|
-
"@types/koa2-cors": "
|
|
54
|
-
"http-proxy-middleware": "
|
|
55
|
-
"koa-body": "
|
|
56
|
-
"koa-mount": "
|
|
57
|
-
"koa-range": "
|
|
58
|
-
"koa-static": "
|
|
59
|
-
"koa2-connect": "
|
|
60
|
-
"koa2-cors": "
|
|
49
|
+
"@koa/multer": "*",
|
|
50
|
+
"@types/koa-mount": "*",
|
|
51
|
+
"@types/koa-range": "*",
|
|
52
|
+
"@types/koa-static": "*",
|
|
53
|
+
"@types/koa2-cors": "*",
|
|
54
|
+
"http-proxy-middleware": "*",
|
|
55
|
+
"koa-body": "*",
|
|
56
|
+
"koa-mount": "*",
|
|
57
|
+
"koa-range": "*",
|
|
58
|
+
"koa-static": "*",
|
|
59
|
+
"koa2-connect": "*",
|
|
60
|
+
"koa2-cors": "*",
|
|
61
61
|
"multer": "*",
|
|
62
|
-
"path-to-regexp": "
|
|
62
|
+
"path-to-regexp": "*"
|
|
63
63
|
},
|
|
64
64
|
"peerDependenciesMeta": {
|
|
65
65
|
"@types/koa-mount": {
|
|
@@ -106,6 +106,15 @@
|
|
|
106
106
|
},
|
|
107
107
|
"@koa/bodyparser": {
|
|
108
108
|
"optional": true
|
|
109
|
+
},
|
|
110
|
+
"http-proxy-middleware": {
|
|
111
|
+
"optional": true
|
|
112
|
+
},
|
|
113
|
+
"koa2-connect": {
|
|
114
|
+
"optional": true
|
|
115
|
+
},
|
|
116
|
+
"path-to-regexp": {
|
|
117
|
+
"optional": true
|
|
109
118
|
}
|
|
110
119
|
},
|
|
111
120
|
"repository": {
|
package/src/KoaApplication.ts
CHANGED
|
@@ -2,7 +2,7 @@ import "reflect-metadata";
|
|
|
2
2
|
import { ApplicationStart, ApplicationStop, Autowired, Log } from "@fastcar/core/annotation";
|
|
3
3
|
import { FastCarApplication, BootPriority, ComponentKind, Logger } from "@fastcar/core";
|
|
4
4
|
import * as Koa from "koa";
|
|
5
|
-
import
|
|
5
|
+
import KoaRouter from "@koa/router";
|
|
6
6
|
import { MethodType } from "./type/MethodType";
|
|
7
7
|
import { DesignMeta } from "./type/DesignMeta";
|
|
8
8
|
import { TypeUtil, ValidationUtil } from "@fastcar/core/utils";
|
|
@@ -53,7 +53,7 @@ export default class KoaApplication {
|
|
|
53
53
|
*
|
|
54
54
|
*/
|
|
55
55
|
protected loadRoute(): Koa.Middleware {
|
|
56
|
-
|
|
56
|
+
const router = new KoaRouter();
|
|
57
57
|
|
|
58
58
|
let instanceList = this.app.getComponentByType(ComponentKind.Controller);
|
|
59
59
|
|
package/target/KoaApplication.js
CHANGED
|
@@ -13,7 +13,7 @@ require("reflect-metadata");
|
|
|
13
13
|
const annotation_1 = require("@fastcar/core/annotation");
|
|
14
14
|
const core_1 = require("@fastcar/core");
|
|
15
15
|
const Koa = require("koa");
|
|
16
|
-
const
|
|
16
|
+
const router_1 = require("@koa/router");
|
|
17
17
|
const DesignMeta_1 = require("./type/DesignMeta");
|
|
18
18
|
const utils_1 = require("@fastcar/core/utils");
|
|
19
19
|
const server_1 = require("@fastcar/server");
|
|
@@ -52,7 +52,7 @@ let KoaApplication = class KoaApplication {
|
|
|
52
52
|
*
|
|
53
53
|
*/
|
|
54
54
|
loadRoute() {
|
|
55
|
-
|
|
55
|
+
const router = new router_1.default();
|
|
56
56
|
let instanceList = this.app.getComponentByType(core_1.ComponentKind.Controller);
|
|
57
57
|
//查找绑定的url
|
|
58
58
|
instanceList.forEach((instance) => {
|
package/test/logs/sys.log
CHANGED
|
@@ -183,3 +183,26 @@
|
|
|
183
183
|
{"timestamp":"2025-12-23 10:59:33.833","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
184
184
|
{"timestamp":"2025-12-23 10:59:33.834","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
185
185
|
{"timestamp":"2025-12-23 10:59:33.835","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
186
|
+
{"timestamp":"2025-12-23 22:14:30.131","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
187
|
+
{"timestamp":"2025-12-23 22:14:33.982","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
188
|
+
{"timestamp":"2025-12-23 22:14:33.984","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
189
|
+
{"timestamp":"2025-12-23 22:14:34.06","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
190
|
+
{"timestamp":"2025-12-23 22:14:34.07","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
191
|
+
{"timestamp":"2025-12-23 22:14:34.09","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
192
|
+
{"timestamp":"2025-12-23 22:29:19.999","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
193
|
+
{"timestamp":"2025-12-23 22:29:20.679","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
194
|
+
{"timestamp":"2025-12-23 22:29:20.681","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
195
|
+
{"timestamp":"2025-12-23 22:29:20.692","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
196
|
+
{"timestamp":"2025-12-23 22:29:20.693","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
197
|
+
{"timestamp":"2025-12-23 22:29:20.694","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
198
|
+
{"timestamp":"2025-12-24 16:51:54.144","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
199
|
+
{"timestamp":"2025-12-24 16:51:55.401","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
200
|
+
{"timestamp":"2025-12-24 16:51:55.401","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
201
|
+
{"timestamp":"2025-12-24 16:51:55.404","level":"ERROR","label":"sys","message":"Unhandled Rejection at:","splat":"[{}]"}
|
|
202
|
+
{"timestamp":"2025-12-24 16:51:55.408","level":"ERROR","label":"sys","message":"reason: Cannot find module 'koa2-cors'\nRequire stack:\n- D:\\code\\fast-car\\fastcar-koa\\src\\middleware\\KoaCors.ts\n- D:\\code\\fast-car\\fastcar-koa\\test\\simple\\app.ts","splat":"[{\"code\":\"MODULE_NOT_FOUND\",\"requireStack\":[\"D:\\\\code\\\\fast-car\\\\fastcar-koa\\\\src\\\\middleware\\\\KoaCors.ts\",\"D:\\\\code\\\\fast-car\\\\fastcar-koa\\\\test\\\\simple\\\\app.ts\"]}]","stack":"Error: Cannot find module 'koa2-cors'\nRequire stack:\n- D:\\code\\fast-car\\fastcar-koa\\src\\middleware\\KoaCors.ts\n- D:\\code\\fast-car\\fastcar-koa\\test\\simple\\app.ts\n at Function.<anonymous> (node:internal/modules/cjs/loader:1401:15)\n at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (D:\\code\\fast-car\\node_modules\\@cspotcode\\source-map-support\\source-map-support.js:811:30)\n at defaultResolveImpl (node:internal/modules/cjs/loader:1057:19)\n at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1062:22)\n at Function._load (node:internal/modules/cjs/loader:1211:37)\n at TracingChannel.traceSync (node:diagnostics_channel:322:14)\n at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)\n at Module.require (node:internal/modules/cjs/loader:1487:12)\n at require (node:internal/modules/helpers:135:16)\n at KoaApplication.KoaCors (D:\\code\\fast-car\\fastcar-koa\\src\\middleware\\KoaCors.ts:6:19)"}
|
|
203
|
+
{"timestamp":"2025-12-24 16:52:22.882","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
204
|
+
{"timestamp":"2025-12-24 16:52:23.412","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
205
|
+
{"timestamp":"2025-12-24 16:52:23.413","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
206
|
+
{"timestamp":"2025-12-24 16:52:49.516","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
207
|
+
{"timestamp":"2025-12-24 16:52:49.517","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
208
|
+
{"timestamp":"2025-12-24 16:52:49.518","level":"INFO","label":"sys","message":"version 1.0.0"}
|