添加接口

This commit is contained in:
2025-12-11 17:30:38 +08:00
parent 0927b5bcd8
commit 5b44cdb3eb
26 changed files with 613 additions and 212 deletions

View File

@@ -42,14 +42,19 @@ app.use('/api/cattle-market-data', createProxyMiddleware({
// API 代理:必须在静态文件服务之前,将 /api 请求代理到 cattletrack.aiotagro.com
app.use('/api', createProxyMiddleware({
// 注意app.use('/api') 会剥离 '/api' 前缀,所以 req.url 变成了 '/login'
// 我们将 target 设置为根域名,并通过 pathRewrite 手动添加 /api 前缀
target: 'https://cattletrack.aiotagro.com',
changeOrigin: true,
secure: false,
pathRewrite: {
'^/api': '/api', // 保持 /api 前缀
'^/': '/api/', // 将 /login 重写为 /api/login
},
onProxyReq: (proxyReq, req, res) => {
console.log(`[代理] ${req.method} ${req.url} -> https://cattletrack.aiotagro.com${req.url}`);
// 这里的 req.url 已经被 Express 剥离了 /api 前缀,例如变为 /login
// pathRewrite 将其变为 /api/login
// target 是 ...com所以最终请求 url 是 ...com/api/login
console.log(`[代理] ${req.method} ${req.originalUrl} -> ${proxyReq.protocol}//${proxyReq.host}${proxyReq.path}`);
// 确保请求方法正确转发
proxyReq.method = req.method;
},
@@ -67,6 +72,7 @@ app.use(express.static(__dirname));
app.listen(PORT, () => {
console.log(`服务器已启动: http://127.0.0.1:${PORT}`);
console.log(`启动时间: ${new Date().toLocaleString()}`);
console.log(`请访问: http://127.0.0.1:${PORT}/index.html`);
});