优化功能
This commit is contained in:
+19
-14
@@ -3,11 +3,8 @@ import * as https from 'https';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
const proxyPort = 9443;
|
||||
const targetHost = "devrmtapp.resmart.cn";
|
||||
|
||||
// 配置文件路径
|
||||
const CONFIG_FILE = path.join(__dirname, 'mock-routes.json');
|
||||
const CONFIG_FILE = path.join(__dirname, 'config.json');
|
||||
|
||||
// 定义类型
|
||||
interface RouteConfig {
|
||||
@@ -18,6 +15,8 @@ interface AppConfig {
|
||||
cacheConfig: boolean;
|
||||
reloadOnChange: boolean;
|
||||
defaultContentType: string;
|
||||
proxyPort: number;
|
||||
targetHost: string;
|
||||
}
|
||||
|
||||
interface ConfigFile {
|
||||
@@ -30,7 +29,9 @@ let MOCK_ROUTES: RouteConfig = {};
|
||||
let CONFIG: AppConfig = {
|
||||
cacheConfig: true,
|
||||
reloadOnChange: true,
|
||||
defaultContentType: "application/json"
|
||||
defaultContentType: "application/json",
|
||||
proxyPort: 9443,
|
||||
targetHost: "devrmtapp.resmart.cn"
|
||||
};
|
||||
|
||||
// 过滤掉以 # 开头的路由(视为注释,不参与 mock)
|
||||
@@ -58,7 +59,9 @@ function loadConfig() {
|
||||
config: {
|
||||
cacheConfig: true,
|
||||
reloadOnChange: true,
|
||||
defaultContentType: "application/json"
|
||||
defaultContentType: "application/json",
|
||||
proxyPort: 9443,
|
||||
targetHost: "devrmtapp.resmart.cn"
|
||||
}
|
||||
};
|
||||
|
||||
@@ -95,7 +98,9 @@ function loadConfig() {
|
||||
CONFIG = {
|
||||
cacheConfig: true,
|
||||
reloadOnChange: true,
|
||||
defaultContentType: "application/json"
|
||||
defaultContentType: "application/json",
|
||||
proxyPort: 9443,
|
||||
targetHost: "devrmtapp.resmart.cn"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -194,13 +199,13 @@ const proxyServer = http.createServer((clientReq, clientRes) => {
|
||||
|
||||
// 目标服务器的选项
|
||||
const options: http.RequestOptions = {
|
||||
hostname: targetHost,
|
||||
hostname: CONFIG.targetHost,
|
||||
port: 443, // HTTPS 默认端口
|
||||
method: clientReq.method,
|
||||
path: parsedUrl.pathname + parsedUrl.search,
|
||||
headers: {
|
||||
...clientReq.headers,
|
||||
host: targetHost
|
||||
host: CONFIG.targetHost
|
||||
}
|
||||
};
|
||||
|
||||
@@ -295,16 +300,16 @@ if (CONFIG.reloadOnChange) {
|
||||
console.log(`[CONFIG] 已启用配置文件监视: ${CONFIG_FILE}`);
|
||||
}
|
||||
|
||||
proxyServer.listen(proxyPort, "0.0.0.0", () => {
|
||||
proxyServer.listen(CONFIG.proxyPort, "0.0.0.0", () => {
|
||||
console.log(`========================================`);
|
||||
console.log(`代理服务器运行在: http://localhost:${proxyPort}`);
|
||||
console.log(`目标服务器: https://${targetHost}`);
|
||||
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(`========================================`);
|
||||
console.log(`管理接口:`);
|
||||
console.log(` GET http://localhost:${proxyPort}/__config 查看当前配置`);
|
||||
console.log(` POST http://localhost:${proxyPort}/__reload-config 重新加载配置`);
|
||||
console.log(` GET http://localhost:${CONFIG.proxyPort}/__config 查看当前配置`);
|
||||
console.log(` POST http://localhost:${CONFIG.proxyPort}/__reload-config 重新加载配置`);
|
||||
console.log(`========================================`);
|
||||
console.log(`Mock路由列表:`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user