解决查询__config报错的问题

This commit is contained in:
tony 2026-04-07 23:42:51 +08:00
parent 3e93594316
commit 0545b4ffcd

View File

@ -166,6 +166,94 @@ const proxyServer = http.createServer((clientReq, clientRes) => {
const parsedUrl = new URL(`http://localhost${clientReq.url!}`); const parsedUrl = new URL(`http://localhost${clientReq.url!}`);
const requestPath = parsedUrl.pathname; const requestPath = parsedUrl.pathname;
// 管理接口:保留路径,不参与代理转发
if (requestPath === "/__reload-config") {
if (clientReq.method !== "POST") {
clientRes.writeHead(405, {
"Content-Type": "application/json",
Allow: "POST",
});
clientRes.end(
JSON.stringify({
success: false,
error: "Method Not Allowed",
allow: ["POST"],
}),
);
return;
}
try {
const oldCount = Object.keys(MOCK_ROUTES).length;
loadConfig();
const newCount = Object.keys(MOCK_ROUTES).length;
clientRes.writeHead(200, { "Content-Type": "application/json" });
clientRes.end(
JSON.stringify({
success: true,
message: "Configuration reloaded successfully",
routesCount: newCount,
routesChanged: newCount - oldCount,
}),
);
console.log(
`[ADMIN] 通过API重新加载配置 (路由数: ${oldCount} -> ${newCount})`,
);
} catch (error) {
clientRes.writeHead(500, { "Content-Type": "application/json" });
clientRes.end(
JSON.stringify({
success: false,
error: (error as Error).message,
}),
);
}
return;
}
if (requestPath === "/__config") {
if (clientReq.method !== "GET") {
clientRes.writeHead(405, {
"Content-Type": "application/json",
Allow: "GET",
});
clientRes.end(
JSON.stringify({
success: false,
error: "Method Not Allowed",
allow: ["GET"],
}),
);
return;
}
try {
clientRes.writeHead(200, { "Content-Type": "application/json" });
clientRes.end(
JSON.stringify(
{
routes: MOCK_ROUTES,
config: CONFIG,
timestamp: new Date().toISOString(),
totalRoutes: Object.keys(MOCK_ROUTES).length,
},
null,
2,
),
);
} catch (error) {
clientRes.writeHead(500, { "Content-Type": "application/json" });
clientRes.end(
JSON.stringify({
success: false,
error: (error as Error).message,
}),
);
}
return;
}
// 检查是否为需要mock的路由 // 检查是否为需要mock的路由
if (isMockRoute(requestPath)) { if (isMockRoute(requestPath)) {
const mockFile = getMockFilePath(requestPath); const mockFile = getMockFilePath(requestPath);
@ -265,69 +353,6 @@ const proxyServer = http.createServer((clientReq, clientRes) => {
clientReq.pipe(proxyReq); clientReq.pipe(proxyReq);
}); });
// 添加管理接口用于重新加载配置
proxyServer.on("request", (req, res) => {
const parsedUrl = new URL(`http://localhost${req.url!}`);
const pathname = parsedUrl.pathname;
// 管理接口:重新加载配置
if (pathname === "/__reload-config" && req.method === "POST") {
try {
const oldCount = Object.keys(MOCK_ROUTES).length;
loadConfig();
const newCount = Object.keys(MOCK_ROUTES).length;
res.writeHead(200, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
success: true,
message: "Configuration reloaded successfully",
routesCount: newCount,
routesChanged: newCount - oldCount,
}),
);
console.log(
`[ADMIN] 通过API重新加载配置 (路由数: ${oldCount} -> ${newCount})`,
);
} catch (error) {
res.writeHead(500, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
success: false,
error: (error as Error).message,
}),
);
}
}
// 管理接口:查看当前配置
if (pathname === "/__config" && req.method === "GET") {
try {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(
JSON.stringify(
{
routes: MOCK_ROUTES,
config: CONFIG,
timestamp: new Date().toISOString(),
totalRoutes: Object.keys(MOCK_ROUTES).length,
},
null,
2,
),
);
} catch (error) {
res.writeHead(500, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
success: false,
error: (error as Error).message,
}),
);
}
}
});
// 初始化:第一次加载配置 // 初始化:第一次加载配置
loadConfig(); loadConfig();