feat(training): 新增培训考证报名与静态资源访问

This commit is contained in:
lik
2026-11-03 20:14:57 +08:00
parent df0752204d
commit c4330e9c77
8 changed files with 428 additions and 10 deletions
+16 -10
View File
@@ -1,25 +1,30 @@
import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import cors from 'koa-cors';
import mount from 'koa-mount';
import serve from 'koa-static';
import http from 'http';
import { fileURLToPath } from 'url';
import { DBModel } from './models/index.js';
import registerRoutes from './routes/index.js';
import ResponseUtil from './utils/responseUtil.js';
// 语音/图片识别走 base64 JSON,单请求体可能达到数 MB;
// 只对该前缀放宽解析上限,其余接口维持 1MB,避免全局放大内存占用
const MEDIA_ROUTE_PREFIX = '/health/ai/';
const STATIC_DIR = fileURLToPath(new URL('./statics', import.meta.url));
// 语音/图片识别与培训报名材料走 base64 JSON,单请求体可能达到数 MB;
// 只对这些前缀放宽解析上限,其余接口维持 1MB,避免全局放大内存占用
const LARGE_BODY_PREFIXES = ['/health/ai/', '/health/training-application'];
const DEFAULT_BODY_LIMIT = '1mb';
const MEDIA_BODY_LIMIT = '20mb';
const LARGE_BODY_LIMIT = '20mb';
const defaultBodyParser = bodyParser({
jsonLimit: DEFAULT_BODY_LIMIT,
formLimit: DEFAULT_BODY_LIMIT,
textLimit: DEFAULT_BODY_LIMIT,
});
const mediaBodyParser = bodyParser({
jsonLimit: MEDIA_BODY_LIMIT,
formLimit: MEDIA_BODY_LIMIT,
textLimit: MEDIA_BODY_LIMIT,
const largeBodyParser = bodyParser({
jsonLimit: LARGE_BODY_LIMIT,
formLimit: LARGE_BODY_LIMIT,
textLimit: LARGE_BODY_LIMIT,
});
class APP {
@@ -41,10 +46,11 @@ class APP {
this.app.proxy = true;
this.app.proxyIpHeader = "X-Real-IP";
this.app.use(mount('/static', serve(STATIC_DIR)));
this.app.use(cors());
this.app.use((ctx, next) => {
const parser = ctx.path.startsWith(MEDIA_ROUTE_PREFIX)
? mediaBodyParser
const parser = LARGE_BODY_PREFIXES.some((prefix) => ctx.path.startsWith(prefix))
? largeBodyParser
: defaultBodyParser;
return parser(ctx, next);
});