-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
1240 lines (1079 loc) · 38.4 KB
/
server.js
File metadata and controls
1240 lines (1079 loc) · 38.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 通用 Mock 服务器 - 主服务器文件
* 接收 Proxyman 转发的请求,根据接口标识符匹配对应的 Mock 响应
*/
const express = require('express');
const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar');
// 使用 ts-node 支持 TypeScript 文件
require('ts-node').register({
transpileOnly: true,
compilerOptions: {
module: 'commonjs',
baseUrl: __dirname,
paths: {
'*': ['*', 'utils/*', 'utils/common/*']
}
}
});
// 设置项目根目录环境变量,供 Mock 文件使用
process.env.JACKY_PROXY_ROOT = __dirname;
const { extractInterfaceIdentifier } = require('./utils/interface-identifier');
const { matchResponse } = require('./utils/common/match-response');
const { generateConfig, validateConfig, mergeConfig } = require('./scripts/generate-config');
const app = express();
const PORT = process.env.PORT || 5001;
// 中间件:解析 JSON 和 URL 编码的请求体(必须在日志中间件之前,以便日志可以打印 body)
// 配置 strict: false 以允许控制字符(如换行符、制表符等)
app.use(express.json({
limit: '50mb',
strict: false
}));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
// JSON 解析错误处理中间件(必须在 express.json() 之后)
app.use((err, req, res, next) => {
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
// 只在 debug 模式打印 JSON 解析错误
const isDebugMode = process.env.DEBUG === 'true' || process.env.DEBUG === '1';
if (isDebugMode) {
console.warn('⚠️ JSON 解析错误:', err.message);
console.warn(' 请求路径:', req.path);
console.warn(' 请求方法:', req.method);
}
// 设置空 body 并继续处理,避免中断请求
req.body = req.body || {};
next();
} else {
next(err);
}
});
/**
* ANSI 颜色代码
*/
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
gray: '\x1b[90m',
white: '\x1b[37m'
};
/**
* 获取格式化的时间戳(带颜色)
*/
function getTimestamp() {
const now = new Date();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
const milliseconds = String(now.getMilliseconds()).padStart(3, '0');
return `${colors.gray}[${hours}:${minutes}:${seconds}.${milliseconds}]${colors.reset}`;
}
/**
* 请求日志中间件
* 必须在所有路由之前应用,确保所有请求都会被记录
*/
function requestLogger(req, res, next) {
const isDebugMode = process.env.DEBUG === 'true' || process.env.DEBUG === '1';
const method = req.method;
const url = req.url;
const path = req.path;
if (isDebugMode) {
// Debug 模式:输出详细信息
const timestamp = new Date().toISOString();
const fullUrl = req.originalUrl || url;
const query = req.query;
const headers = req.headers;
const body = req.body;
const ip = req.ip || req.connection.remoteAddress;
console.log('\n========== 请求拦截 ==========');
console.log(`时间: ${timestamp}`);
console.log(`方法: ${method}`);
console.log(`URL: ${url}`);
console.log(`完整路径: ${fullUrl}`);
console.log(`路径: ${path}`);
console.log(`Headers:`, JSON.stringify(headers, null, 2));
if (Object.keys(query).length > 0) {
console.log(`Query参数:`, JSON.stringify(query, null, 2));
}
if (body && Object.keys(body).length > 0) {
console.log(`Body:`, JSON.stringify(body, null, 2));
}
console.log(`IP: ${ip}`);
console.log('================================\n');
} else {
// 精简模式:只输出关键信息(带时间戳和颜色)
const methodColor = method === 'GET' ? colors.cyan : method === 'POST' ? colors.blue : colors.white;
console.log(`${getTimestamp()} ${methodColor}${method}${colors.reset} ${colors.dim}${path}${colors.reset}`);
}
next();
}
// 应用请求日志中间件(在所有路由之前)
app.use(requestLogger);
// 静态文件服务(Web 界面)- 放在日志中间件之后,这样静态文件请求也会被记录
app.use(express.static(path.join(__dirname, 'public')));
// 加载配置文件
let mockConfig = null;
let currentMockId = null;
/**
* 获取工作目录(数据存储目录)
* 如果设置了 WORK_DIR 环境变量,使用工作目录;否则使用项目根目录
*/
function getWorkDir() {
return process.env.WORK_DIR || __dirname;
}
/**
* 加载 proxy.config.json 配置文件
*/
function loadMockConfig() {
try {
// 优先使用环境变量指定的配置文件路径
let configPath;
if (process.env.CONFIG_PATH) {
configPath = process.env.CONFIG_PATH;
} else {
// 否则使用工作目录的配置文件
const workDir = getWorkDir();
configPath = path.join(workDir, 'proxy.config.json');
}
// 确保路径是绝对路径
if (!path.isAbsolute(configPath)) {
configPath = path.resolve(configPath);
}
if (fs.existsSync(configPath)) {
const configContent = fs.readFileSync(configPath, 'utf-8');
mockConfig = JSON.parse(configContent);
console.log(`✅ 已加载配置文件: ${configPath}`);
return mockConfig;
} else {
console.warn(`配置文件不存在: ${configPath}`);
if (process.env.CONFIG_PATH) {
console.warn(` CONFIG_PATH 环境变量: ${process.env.CONFIG_PATH}`);
}
if (process.env.WORK_DIR) {
console.warn(` WORK_DIR 环境变量: ${process.env.WORK_DIR}`);
}
return null;
}
} catch (error) {
console.error('加载配置文件失败:', error);
return null;
}
}
/**
* 根据 mockId 获取接口集路径
*/
function getMockFolderPath(mockId) {
if (!mockConfig) {
loadMockConfig();
}
if (!mockConfig || !mockConfig.folders || !mockConfig.folders.list) {
return null;
}
const folder = mockConfig.folders.list.find(f => f.id === parseInt(mockId));
if (!folder) {
return null;
}
// 使用工作目录作为基础路径
const workDir = getWorkDir();
const folderPath = path.join(workDir, folder.path);
return folderPath;
}
/**
* 扫描目录,加载所有 .mock.ts 文件
*/
function loadMockFiles(folderPath) {
const mockFiles = {};
if (!fs.existsSync(folderPath)) {
console.warn(`${colors.yellow}⚠️ 接口集路径不存在:${colors.reset} ${colors.dim}${folderPath}${colors.reset}`);
return mockFiles;
}
// 获取项目根目录,用于模块解析
const projectRoot = __dirname;
// 保存原始的 NODE_PATH
const originalNodePath = process.env.NODE_PATH || '';
// 将项目根目录添加到 NODE_PATH,这样 Mock 文件可以导入项目根目录的模块
const nodePaths = originalNodePath ? originalNodePath.split(path.delimiter) : [];
if (!nodePaths.includes(projectRoot)) {
nodePaths.unshift(projectRoot);
process.env.NODE_PATH = nodePaths.join(path.delimiter);
}
function scanDirectory(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// 递归扫描子目录
scanDirectory(filePath);
} else if (file.endsWith('.mock.ts') || file.endsWith('.mock.js')) {
// 从文件名提取接口标识符(去掉 .mock.ts 后缀)
const interfaceName = file.replace(/\.mock\.(ts|js)$/, '');
try {
// 动态加载 Mock 文件
// 注意:这里需要支持 TypeScript,可以使用 ts-node 或先编译
delete require.cache[require.resolve(filePath)];
const mockModule = require(filePath);
// 支持 default 导出或直接导出函数
const mockHandler = mockModule.default || mockModule;
if (typeof mockHandler === 'function') {
mockFiles[interfaceName] = mockHandler;
// 保存该 handler 对应的本地文件路径(绝对路径)
mockFilePaths[interfaceName] = filePath;
console.log(`${colors.green}✓${colors.reset} ${colors.dim}加载 Mock 文件:${colors.reset} ${colors.cyan}${file}${colors.reset} ${colors.gray}->${colors.reset} ${colors.bright}${interfaceName}${colors.reset}`);
} else {
console.warn(`${colors.yellow}⚠${colors.reset} ${colors.yellow}Mock 文件 ${colors.cyan}${file}${colors.yellow} 未导出函数${colors.reset}`);
}
} catch (error) {
console.error(`${colors.red}✗${colors.reset} ${colors.red}加载 Mock 文件失败:${colors.reset} ${colors.cyan}${file}${colors.reset} ${colors.red}${error.message}${colors.reset}`);
// 打印更详细的错误信息
if (error.code === 'MODULE_NOT_FOUND') {
console.error(` 无法找到模块: ${error.message}`);
console.error(` Mock 文件路径: ${filePath}`);
console.error(` 项目根目录: ${projectRoot}`);
}
}
}
}
}
try {
scanDirectory(folderPath);
} finally {
// 恢复原始的 NODE_PATH
process.env.NODE_PATH = originalNodePath;
}
return mockFiles;
}
// 缓存 Mock 文件
let cachedMockFiles = {};
let cachedMockId = null;
// 存储 mock 处理器对应的本地文件绝对路径(用于前端复制)
let mockFilePaths = {};
// 存储动态切换的场景配置(用于接口的场景切换)
// 格式: { 'getProductInfo': '2' } 表示 getProductInfo 使用 getProductInfo-2.mock.ts
let mockScenarios = {};
// 存储被禁用的接口(Set)
// 格式: Set(['getProductInfo', 'productSearch']) 表示这些接口被禁用
let disabledInterfaces = new Set();
// 文件监听器
let fileWatcher = null;
/**
* 清除模块及其所有依赖的缓存
*/
function clearModuleCache(modulePath) {
const resolvedPath = require.resolve(modulePath);
// 清除该模块的缓存
if (require.cache[resolvedPath]) {
const module = require.cache[resolvedPath];
// 递归清除所有子模块的缓存
if (module.children) {
module.children.forEach(child => {
if (child.filename && (child.filename.endsWith('.json') || child.filename.includes('base-data'))) {
delete require.cache[child.filename];
}
});
}
delete require.cache[resolvedPath];
}
}
/**
* 清除 base-data 目录下所有 JSON 文件的缓存
*/
function clearBaseDataCache(interfaceName) {
const workDir = getWorkDir();
const baseDataDir = path.join(workDir, 'base-data', interfaceName);
if (fs.existsSync(baseDataDir)) {
const files = fs.readdirSync(baseDataDir);
files.forEach(file => {
if (file.endsWith('.json')) {
const jsonPath = path.join(baseDataDir, file);
try {
const resolvedPath = require.resolve(jsonPath);
delete require.cache[resolvedPath];
} catch (e) {
// 如果文件还未被 require,忽略错误
}
}
});
}
}
/**
* 重新加载单个 Mock 文件
*/
function reloadMockFile(filePath, interfaceName) {
try {
// 清除 base-data 中该接口的所有 JSON 文件缓存
clearBaseDataCache(interfaceName);
// 清除 Mock 文件及其所有依赖的缓存
clearModuleCache(filePath);
// 清除 ts-node 的编译缓存(如果存在)
if (require.extensions['.ts']) {
// ts-node 可能会缓存编译结果,清除相关缓存
const tsNodeCache = require.cache;
Object.keys(tsNodeCache).forEach(key => {
if (key.includes(filePath) || key.includes(interfaceName)) {
delete tsNodeCache[key];
}
});
}
// 重新加载
const mockModule = require(filePath);
const mockHandler = mockModule.default || mockModule;
if (typeof mockHandler === 'function') {
cachedMockFiles[interfaceName] = mockHandler;
mockFilePaths[interfaceName] = filePath;
console.log(`${colors.green}🔄${colors.reset} ${colors.dim}热更新 Mock 文件:${colors.reset} ${colors.cyan}${path.basename(filePath)}${colors.reset} ${colors.gray}->${colors.reset} ${colors.bright}${interfaceName}${colors.reset}`);
return true;
} else {
console.warn(`${colors.yellow}⚠${colors.reset} ${colors.yellow}Mock 文件 ${colors.cyan}${path.basename(filePath)}${colors.yellow} 未导出函数${colors.reset}`);
return false;
}
} catch (error) {
console.error(`${colors.red}✗${colors.reset} ${colors.red}热更新 Mock 文件失败:${colors.reset} ${colors.cyan}${path.basename(filePath)}${colors.reset} ${colors.red}${error.message}${colors.reset}`);
if (error.stack) {
console.error(error.stack);
}
return false;
}
}
/**
* 重新加载所有 Mock 文件(当 base-data 变化时)
*/
function reloadAllMockFiles() {
if (!cachedMockId) return;
const folderPath = getMockFolderPath(cachedMockId);
if (!folderPath) return;
console.log(`\n${colors.blue}🔄${colors.reset} ${colors.bright}热更新所有 Mock 文件...${colors.reset}`);
cachedMockFiles = loadMockFiles(folderPath);
console.log(`${colors.green}✅${colors.reset} ${colors.bright}热更新完成,共 ${colors.green}${Object.keys(cachedMockFiles).length}${colors.reset} ${colors.bright}个 Mock 接口${colors.reset}\n`);
}
/**
* 设置文件监听
*/
function setupFileWatcher(mockId) {
// 清除旧的监听器
if (fileWatcher) {
fileWatcher.close();
fileWatcher = null;
}
const folderPath = getMockFolderPath(mockId);
if (!folderPath) return;
const workDir = getWorkDir();
const baseDataPath = path.join(workDir, 'base-data');
const mocksPath = folderPath;
// 要监听的路径
const watchPaths = [];
if (fs.existsSync(baseDataPath)) {
watchPaths.push(baseDataPath);
}
if (fs.existsSync(mocksPath)) {
watchPaths.push(mocksPath);
}
if (watchPaths.length === 0) return;
// 使用 chokidar 监听文件变化
fileWatcher = chokidar.watch(watchPaths, {
ignored: /(^|[\/\\])\../, // 忽略隐藏文件
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 200,
pollInterval: 100
}
});
fileWatcher
.on('change', (filePath) => {
// 延迟处理,避免文件写入未完成
setTimeout(() => {
const ext = path.extname(filePath);
const fileName = path.basename(filePath);
// 如果是 Mock 文件变化
if (filePath.endsWith('.mock.ts') || filePath.endsWith('.mock.js')) {
const interfaceName = fileName.replace(/\.mock\.(ts|js)$/, '');
reloadMockFile(filePath, interfaceName);
}
// 如果是 base-data 中的 JSON 文件变化
else if (ext === '.json' && filePath.includes('base-data')) {
// 找到对应的接口名(从路径中提取)
const baseDataMatch = filePath.match(/base-data[\/\\]([^\/\\]+)/);
if (baseDataMatch) {
const interfaceName = baseDataMatch[1];
// 先清除该 JSON 文件的缓存
try {
const resolvedPath = require.resolve(filePath);
delete require.cache[resolvedPath];
} catch (e) {
// 如果文件还未被 require,忽略错误
}
// 清除该接口的所有 base-data 缓存
clearBaseDataCache(interfaceName);
// 重新加载该接口的 Mock 文件(因为 Mock 文件会 import base-data)
const mockFilePath = path.join(mocksPath, `${interfaceName}.mock.ts`);
if (fs.existsSync(mockFilePath)) {
reloadMockFile(mockFilePath, interfaceName);
} else {
// 如果找不到对应的 Mock 文件,重新加载所有(以防有依赖关系)
reloadAllMockFiles();
}
} else {
// 无法确定接口名,重新加载所有
reloadAllMockFiles();
}
}
}, 100);
})
.on('error', (error) => {
console.error(`${colors.red}✗${colors.reset} ${colors.red}文件监听错误:${colors.reset} ${error.message}`);
});
console.log(`${colors.blue}👁️${colors.reset} ${colors.dim}已启用文件热更新监听${colors.reset}`);
}
/**
* 获取或加载 Mock 文件
*/
function getMockFiles(mockId) {
if (cachedMockId === mockId && Object.keys(cachedMockFiles).length > 0) {
return cachedMockFiles;
}
const folderPath = getMockFolderPath(mockId);
if (!folderPath) {
console.error(`未找到 mockId ${mockId} 对应的接口集`);
return {};
}
console.log(`\n${colors.blue}📂${colors.reset} ${colors.bright}加载接口集:${colors.reset} ${colors.dim}${folderPath}${colors.reset}`);
cachedMockFiles = loadMockFiles(folderPath);
cachedMockId = mockId;
console.log(`${colors.green}✅${colors.reset} ${colors.bright}共加载 ${colors.green}${Object.keys(cachedMockFiles).length}${colors.reset} ${colors.bright}个 Mock 接口${colors.reset}\n`);
// 设置文件监听
setupFileWatcher(mockId);
return cachedMockFiles;
}
/**
* 接口识别配置(可以从配置文件加载,这里使用默认配置)
*/
const interfaceIdentifierConfig = {
strategies: [
{
type: 'urlPattern',
pattern: '/([^/]+)$',
group: 1,
description: '提取 URL 最后一段作为接口标识符'
},
{
type: 'header',
key: 'X-Interface-Name',
description: '从请求头提取接口标识符'
}
]
};
/**
* 处理所有 HTTP 请求
*/
async function handleRequest(req, res) {
try {
// 从请求中提取接口标识符
const interfaceName = extractInterfaceIdentifier(req, interfaceIdentifierConfig);
if (!interfaceName) {
console.log('⚠️ 无法从请求中提取接口标识符,返回原始请求信息');
return res.status(400).json({
error: true,
message: '无法识别接口标识符',
request: {
method: req.method,
url: req.url,
path: req.path,
headers: req.headers,
query: req.query,
body: req.body
}
});
}
// 只在 debug 模式打印接口标识符
const isDebugMode = process.env.DEBUG === 'true' || process.env.DEBUG === '1';
if (isDebugMode) {
console.log(`🔍 识别到接口标识符: ${interfaceName}`);
}
// 检查接口是否被禁用
if (disabledInterfaces.has(interfaceName)) {
if (isDebugMode) {
console.log(`🚫 接口 ${interfaceName} 已被禁用,跳过 mock 处理`);
}
return res.json({
message: `接口 ${interfaceName} 已被禁用`,
disabled: true,
interfaceName
});
}
// 获取当前 mockId(从环境变量或命令行参数)
const mockId = currentMockId || process.env.MOCK_ID || '1';
// 获取 Mock 文件
const mockFiles = getMockFiles(mockId);
// 检查是否需要动态切换 mock 场景(通过 CLI 或 Web 界面设置)
// 支持所有接口的场景切换,例如: getProductInfo-2, getProductInfo-3 等
let actualInterfaceName = interfaceName;
const scenario = mockScenarios[interfaceName];
if (scenario) {
const scenarioInterfaceName = `${interfaceName}-${scenario}`;
if (mockFiles[scenarioInterfaceName]) {
actualInterfaceName = scenarioInterfaceName;
if (isDebugMode) {
console.log(`🔄 使用动态切换的 mock 场景: ${interfaceName} -> ${actualInterfaceName}`);
}
} else {
if (isDebugMode) {
console.log(`⚠️ 未找到场景 ${scenario} 的 mock 处理器 (${scenarioInterfaceName}),使用默认 ${interfaceName}`);
}
delete mockScenarios[interfaceName]; // 清除无效的场景配置
}
}
// 查找对应的 mock 处理器
const mockHandler = mockFiles[actualInterfaceName];
if (!mockHandler) {
// 使用静态变量记录已警告的接口,避免重复打印
if (!handleRequest.warnedInterfaces) {
handleRequest.warnedInterfaces = new Set();
}
if (!handleRequest.warnedInterfaces.has(actualInterfaceName)) {
handleRequest.warnedInterfaces.add(actualInterfaceName);
console.log(`${getTimestamp()} ${colors.yellow}⚠️ ${actualInterfaceName} 未找到 mock 处理器${colors.reset}`);
// 只在 debug 模式打印可用的处理器列表
if (isDebugMode) {
console.log(`${colors.dim}📋 可用的 mock 处理器: ${Object.keys(mockFiles).join(', ')}${colors.reset}`);
}
}
return res.status(404).json({
error: true,
message: `未找到 ${actualInterfaceName} 的 Mock 文件`,
interfaceName,
actualInterfaceName,
availableInterfaces: Object.keys(mockFiles)
});
}
// 确保 mockHandler 是函数
if (typeof mockHandler !== 'function') {
console.error(`❌ mockHandler 不是函数,实际类型: ${typeof mockHandler}`);
console.error(` 值:`, mockHandler);
return res.status(500).json({
error: 'Mock 处理器不是函数',
interfaceName: actualInterfaceName,
handlerType: typeof mockHandler
});
}
// 构建请求对象
const request = {
body: req.body,
options: {
headers: req.headers
},
method: req.method,
url: req.url,
path: req.path,
query: req.query
};
// 调用 Mock 处理函数(只在 debug 模式打印详细信息)
if (isDebugMode) {
console.log(`🚀 调用 ${actualInterfaceName} 的 mock 处理器...`);
if (actualInterfaceName !== interfaceName) {
console.log(` (原始接口标识符: ${interfaceName})`);
}
console.log(` 处理器类型: ${typeof mockHandler}`);
}
let result;
try {
result = await mockHandler(request);
} catch (error) {
console.error(`❌ Mock 处理器执行出错:`, error);
return res.status(500).json({
error: 'Mock 处理器执行出错',
message: error.message,
interfaceName: actualInterfaceName
});
}
// 检查是否匹配失败
// matchResponse 返回错误时的格式:{ error: true, message: '...', ... }
// 需要检查 error 是否为布尔值 true,而不是仅仅检查 error 字段是否存在
// 因为响应数据中可能包含 error 字段(如 { error: { code: '', message: '' } })
if (result && result.body && result.body.error === true) {
console.log(`${getTimestamp()} ${colors.red}❌ ${actualInterfaceName} 匹配失败${colors.reset}`);
return res.status(404).json(result.body);
}
// 返回响应
if (result && result.body) {
// 设置响应头
if (result.headers) {
Object.keys(result.headers).forEach(key => {
res.setHeader(key, result.headers[key]);
});
}
// 确保 Content-Type 是 application/json
if (!res.getHeader('Content-Type')) {
res.setHeader('Content-Type', 'application/json');
}
// 优先使用 result.status,如果没有则尝试从响应体中提取
let status = result.status;
if (!status && result.body?.ResponseStatus?.Errors?.[0]?.ErrorCode) {
// ErrorCode 可能是字符串,需要转换为数字
const errorCode = result.body.ResponseStatus.Errors[0].ErrorCode;
status = typeof errorCode === 'string' ? parseInt(errorCode, 10) : errorCode;
}
status = status || 200;
// 精简输出:只显示接口名和状态码(带颜色)
const statusColor = status >= 200 && status < 300 ? colors.green : status >= 400 ? colors.red : colors.yellow;
console.log(`${getTimestamp()} ${colors.green}✅${colors.reset} ${colors.bright}${actualInterfaceName}${colors.reset} ${statusColor}(${status})${colors.reset}`);
// 对于 429 等特殊状态码,使用 send 而不是 json,确保返回实际的响应体内容
// 而不是 Express 默认的状态码文本(如 "Too Many Requests")
if (status === 429) {
return res.status(status).type('json').send(JSON.stringify(result.body));
}
return res.status(status).json(result.body);
} else if (result && result.status) {
// 兼容旧格式:只有 status 没有 body
if (result.headers) {
Object.keys(result.headers).forEach(key => {
res.setHeader(key, result.headers[key]);
});
}
return res.status(result.status).json(result.body || {});
} else {
// 如果没有标准格式,尝试直接返回结果
console.log('⚠️ mock 处理器返回的响应格式不标准,尝试直接返回');
return res.status(200).json(result);
}
} catch (error) {
console.error('❌ 处理请求失败:', error);
return res.status(500).json({
error: true,
message: error.message,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
});
}
}
// 配置管理 API - 获取配置列表
app.get('/api/config', (req, res) => {
try {
const workDir = getWorkDir();
const configPath = path.join(workDir, 'proxy.config.json');
if (fs.existsSync(configPath)) {
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
res.json(config);
} else {
res.json({ libraryId: 2773, folders: { list: [] } });
}
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
// 配置管理 API - 添加配置
app.post('/api/config', (req, res) => {
try {
const workDir = getWorkDir();
const configPath = path.join(workDir, 'proxy.config.json');
let config = { libraryId: 2773, folders: { list: [] } };
if (fs.existsSync(configPath)) {
config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
}
const { id, path: folderPath, name } = req.body;
// 检查 ID 是否已存在
if (config.folders.list.find(f => f.id === parseInt(id))) {
return res.status(400).json({
success: false,
error: `ID ${id} 已存在`
});
}
config.folders.list.push({ id: parseInt(id), path: folderPath, name });
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8');
res.json({ success: true, config });
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
// 配置管理 API - 删除配置
app.delete('/api/config/:id', (req, res) => {
try {
const workDir = getWorkDir();
const configPath = path.join(workDir, 'proxy.config.json');
if (!fs.existsSync(configPath)) {
return res.status(404).json({
success: false,
error: '配置文件不存在'
});
}
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
const id = parseInt(req.params.id);
const index = config.folders.list.findIndex(f => f.id === id);
if (index === -1) {
return res.status(404).json({
success: false,
error: `ID ${id} 不存在`
});
}
config.folders.list.splice(index, 1);
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8');
res.json({ success: true, config });
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
// 配置管理 API - 生成配置
app.post('/api/config/generate', (req, res) => {
try {
const workDir = getWorkDir();
const options = {
rootDir: req.body.rootDir || workDir,
outputPath: path.join(workDir, 'proxy.config.json'),
libraryId: req.body.libraryId || 2773,
startId: req.body.startId || 1
};
const config = generateConfig(options);
res.json({ success: true, config });
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
app.get('/api/config/validate', (req, res) => {
try {
const workDir = getWorkDir();
const configPath = path.join(workDir, 'proxy.config.json');
const result = validateConfig(configPath);
res.json(result);
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
app.post('/api/config/merge', (req, res) => {
try {
const workDir = getWorkDir();
const configPath = path.join(workDir, 'proxy.config.json');
// 加载现有配置
let existingConfig = { libraryId: 2773, folders: { list: [] } };
if (fs.existsSync(configPath)) {
existingConfig = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
}
// 生成新配置
const options = {
rootDir: req.body.rootDir || workDir,
libraryId: existingConfig.libraryId
};
const newConfig = generateConfig({ ...options, outputPath: null });
// 合并配置
const mergedConfig = mergeConfig(existingConfig, newConfig);
// 保存合并后的配置
fs.writeFileSync(configPath, JSON.stringify(mergedConfig, null, 2), 'utf-8');
res.json({ success: true, config: mergedConfig });
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
// 服务器信息 API
app.get('/api/server/info', (req, res) => {
try {
res.json({
port: PORT,
mockId: currentMockId || process.env.MOCK_ID || '1',
loadedInterfaces: Object.keys(cachedMockFiles).length,
availableInterfaces: Object.keys(cachedMockFiles)
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
// ==================== Mock 场景管理 API ====================
/**
* 获取所有可用的 mockId 列表(支持搜索)
*/
app.get('/mock-admin/mockids', (req, res) => {
try {
if (!mockConfig) {
loadMockConfig();
}
const configs = mockConfig?.folders?.list || [];
const currentMockIdNum = currentMockId ? parseInt(currentMockId) : null;
const search = req.query.search || '';
let filteredConfigs = configs;
if (search) {
const searchLower = search.toLowerCase();
filteredConfigs = configs.filter(config => {
const pathMatch = config.path.toLowerCase().includes(searchLower);
const idMatch = String(config.id).includes(search);
return pathMatch || idMatch;
});
}
const workDir = getWorkDir();
const mockIds = filteredConfigs.map(config => {
const folderPath = path.join(workDir, config.path);
return {
id: config.id,
path: config.path,
isActive: currentMockIdNum === config.id,
exists: fs.existsSync(folderPath)
};
});
res.json({
current: currentMockIdNum,
available: mockIds,
total: configs.length,
filtered: filteredConfigs.length
});
} catch (error) {
res.status(500).json({
error: '获取 mockId 列表失败',
message: error.message
});
}
});
/**
* 切换 mockId
*/
app.post('/mock-admin/mockid/:mockId', async (req, res) => {
const { mockId: newMockId } = req.params;
try {
const folderPath = getMockFolderPath(newMockId);
if (!folderPath) {
return res.status(400).json({
error: '切换 mockId 失败',
message: `未找到 mockId ${newMockId} 对应的接口集`
});
}
// 加载新的 mock 文件
getMockFiles(newMockId);
// 更新 mockId
currentMockId = newMockId;
console.log(`\n🔄 已切换 mockId 到 ${newMockId} (${folderPath})`);
res.json({