新增全局开启关闭 mock数据

This commit is contained in:
2026-04-07 11:35:04 +08:00
parent da026bf306
commit 099fd3c62e
3 changed files with 298 additions and 2 deletions
+5 -2
View File
@@ -12,6 +12,8 @@ interface RouteConfig {
}
interface AppConfig {
/** 为 false 时所有请求走代理,不命中 mock 路由;缺省为 true */
mockEnabled?: boolean;
cacheConfig: boolean;
reloadOnChange: boolean;
defaultContentType: string;
@@ -83,7 +85,7 @@ function loadConfig() {
const configData: ConfigFile = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf-8'));
MOCK_ROUTES = filterActiveRoutes(configData.routes || {});
CONFIG = configData.config || CONFIG;
console.log(`[CONFIG] 配置文件已加载,共 ${Object.keys(MOCK_ROUTES).length} 个路由`);
console.log(`[CONFIG] 配置文件已加载,共 ${Object.keys(MOCK_ROUTES).length} 个路由${CONFIG.mockEnabled !== false ? '' : '(mock 已关闭,全部走代理)'}`);
}
// 验证mock文件是否存在
@@ -127,6 +129,7 @@ function validateMockFiles() {
// 检查是否为mock路由的函数
function isMockRoute(requestPath: string): boolean {
if (CONFIG.mockEnabled === false) return false;
return MOCK_ROUTES.hasOwnProperty(requestPath);
}
@@ -305,7 +308,7 @@ proxyServer.listen(CONFIG.proxyPort, "0.0.0.0", () => {
console.log(`代理服务器运行在: http://localhost:${CONFIG.proxyPort}`);
console.log(`目标服务器: https://${CONFIG.targetHost}`);
console.log(`配置文件: ${CONFIG_FILE}`);
console.log(`已配置Mock路由: ${Object.keys(MOCK_ROUTES).length} 个`);
console.log(`已配置Mock路由: ${Object.keys(MOCK_ROUTES).length} 个${CONFIG.mockEnabled !== false ? '' : '(当前 mockEnabled=false,未生效)'}`);
console.log(`========================================`);
console.log(`管理接口:`);
console.log(` GET http://localhost:${CONFIG.proxyPort}/__config 查看当前配置`);