2026-09-18 08:18:30 +08:00
|
|
|
|
// services/api/src/shared/configure-app.ts —— 应用级配置(全局前缀 / CORS /
|
|
|
|
|
|
// 响应头),main-cn.ts 生产引导与 e2e 测试装配共用同一函数:此前 e2e 各 spec
|
|
|
|
|
|
// 手工镜像 setGlobalPrefix,两处漂移无从守护;收敛为单一来源后,e2e 对
|
|
|
|
|
|
// x-powered-by 头的断言才真正落在生产引导路径上(P1a 终审修缮②)。
|
|
|
|
|
|
// 参数类型取 NestExpressApplication:disable() 是 Express 适配器特有方法。
|
|
|
|
|
|
import type { NestExpressApplication } from '@nestjs/platform-express';
|
|
|
|
|
|
import { API_PREFIX } from './constants';
|
|
|
|
|
|
|
|
|
|
|
|
export function configureApp(app: NestExpressApplication): void {
|
|
|
|
|
|
app.setGlobalPrefix(API_PREFIX);
|
|
|
|
|
|
// 隐藏 Express x-powered-by 指纹响应头(终审修缮②,e2e 断言之)。
|
|
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
|
const corsOrigins = (process.env.CORS_ORIGINS ?? 'http://localhost:5173')
|
|
|
|
|
|
.split(',')
|
|
|
|
|
|
.map((origin) => origin.trim())
|
|
|
|
|
|
.filter((origin) => origin !== '');
|
2026-09-20 23:02:07 +08:00
|
|
|
|
// exposedHeaders:嵌入轮播 iframe(跨源 fetch 公开展示位端点)需读 ETag 响应头
|
|
|
|
|
|
// 维持 If-None-Match 条件请求(P4b Task 4)——CORS 缺省只暴露 simple headers,
|
|
|
|
|
|
// 不加此配置浏览器侧 response.headers.get('etag') 恒为 null(304 语义失效)。
|
|
|
|
|
|
app.enableCors({ origin: corsOrigins, exposedHeaders: ['ETag'] });
|
2026-09-18 08:18:30 +08:00
|
|
|
|
}
|