Files
rn-app/scripts/proxy-server.js

120 lines
3.8 KiB
JavaScript
Raw Normal View History

2025-11-05 17:24:55 +08:00
/**
* 开发环境代理服务器
* 用于解决跨域问题和统一 API 请求
*/
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
2025-11-06 16:37:01 +08:00
const cors = require('cors');
2025-11-05 17:24:55 +08:00
const app = express();
2025-11-06 16:37:01 +08:00
const PORT = 8086;
// 启用 CORS
2025-11-13 16:47:10 +08:00
app.use(
cors({
origin: '*', // 允许所有源,开发环境使用
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'],
allowedHeaders: [
'Content-Type',
'Authorization',
'X-Requested-With',
'Accept',
// 自定义请求头
'cmdId',
'datetime',
'pwds',
'aseqId',
'nc',
'apiName',
'tid',
'custId',
'reqId',
'isMobileOpen',
'languageNum',
'project',
'platform',
'checkOr',
'tbc',
'reqKey',
'signature',
'authorization',
],
exposedHeaders: ['cmdId', 'datetime', 'pwds', 'aseqId', 'nc', 'checkOr', 'checkor'],
})
);
2025-11-05 17:24:55 +08:00
// 目标 API 服务器地址
2025-11-06 16:37:01 +08:00
const API_TARGET = process.env.API_TARGET || 'https://51zhh5.notbug.org';
// 代理路径列表
2025-11-11 18:48:54 +08:00
const PROXY_PATHS = ['/api/v2'];
2025-11-06 16:37:01 +08:00
// 为每个路径配置代理
PROXY_PATHS.forEach((path) => {
app.use(
path,
createProxyMiddleware({
target: API_TARGET,
changeOrigin: true,
secure: false, // 如果目标服务器使用自签名证书,设置为 false
pathRewrite: (pathStr, req) => {
// Express 会自动去掉匹配的前缀,所以需要加回来
const fullPath = path + pathStr;
console.log(`[Proxy] Path rewrite: ${pathStr}${fullPath}`);
return fullPath;
},
onProxyReq: (proxyReq, req, res) => {
const fullPath = path + req.url;
console.log(`[Proxy] ${req.method} ${fullPath}${API_TARGET}${fullPath}`);
},
onProxyRes: (proxyRes, req, res) => {
const fullPath = path + req.url;
console.log(`[Proxy] ${req.method} ${fullPath}${proxyRes.statusCode}`);
// 确保 CORS 头被正确设置
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS, PATCH');
2025-11-13 16:47:10 +08:00
res.setHeader(
'Access-Control-Allow-Headers',
'Content-Type, Authorization, X-Requested-With, Accept, cmdId, datetime, pwds, aseqId, nc, apiName, tid, custId, reqId, isMobileOpen, languageNum, project, platform, checkOr, tbc, reqKey, signature, authorization'
);
res.setHeader(
'Access-Control-Expose-Headers',
'cmdId, datetime, pwds, aseqId, nc, checkOr, checkor'
);
2025-11-06 16:37:01 +08:00
res.setHeader('Access-Control-Allow-Credentials', 'true');
},
onError: (err, req, res) => {
console.error('[Proxy Error]', err.message);
res.status(500).json({
error: 'Proxy Error',
message: err.message,
});
},
})
);
});
2025-11-05 17:24:55 +08:00
// 健康检查
app.get('/health', (req, res) => {
res.json({
status: 'ok',
proxy: API_TARGET,
timestamp: new Date().toISOString(),
});
});
app.listen(PORT, () => {
console.log(`
2025-11-07 15:03:00 +08:00
Proxy Server Running
2025-11-05 17:24:55 +08:00
2025-11-07 15:03:00 +08:00
Local: http://localhost:${PORT} ║
Target: ${API_TARGET.padEnd(40)}
Health: http://localhost:${PORT}/health ║
2025-11-05 17:24:55 +08:00
`);
});