const express = require('express'); const { createProxyMiddleware } = require('http-proxy-middleware'); const cors = require('cors'); const app = express(); const PORT = process.env.PROXY_PORT || 3000; app.use(cors()); app.use(express.json()); app.use('/api/chat', createProxyMiddleware({ target: 'https://thinkflow.lz-t.top', changeOrigin: true, pathRewrite: { '^/api/chat': '/chat/completions' }, onProxyReq: (proxyReq, req, res) => { const customBaseUrl = req.headers['x-custom-base-url']; if (customBaseUrl) { const url = new URL(customBaseUrl); proxyReq.path = url.pathname; proxyReq.setHeader('Host', url.host); } } })); app.use('/api/image', createProxyMiddleware({ target: 'https://thinkflow.lz-t.top', changeOrigin: true, pathRewrite: { '^/api/image': '/images/generations' }, onProxyReq: (proxyReq, req, res) => { const customBaseUrl = req.headers['x-custom-base-url']; if (customBaseUrl) { const url = new URL(customBaseUrl); proxyReq.path = url.pathname; proxyReq.setHeader('Host', url.host); } } })); app.listen(PORT, () => { console.log(`Proxy server running on port ${PORT}`); });