-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy-server.js
More file actions
115 lines (102 loc) · 3.21 KB
/
proxy-server.js
File metadata and controls
115 lines (102 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* FORAY API Proxy Server
* Version: 1.2.0
* Created: 2026-02-02T00:00:00Z
* Modified: 2026-02-05T09:40:00Z
*
* Author: Marvin Percival
* Email: marvinp@dunin7.com
* GitHub: github.com/DUNIN7/foray-kaspathon
*
* Proxies API requests from Cloudflare Tunnel to the local API server.
* Handles CORS for browser requests.
*
* Changelog:
* v1.2.0 (2026-02-05): Added /api/describe-transaction endpoint forwarding (Be Creative feature)
* v1.1.0 (2026-02-02): Added /api/generate-foray endpoint forwarding
* v1.0.0: Initial version with /api/analyze-business only
*/
const http = require('http');
// Helper function to proxy requests to backend
function proxyToBackend(req, res, path) {
let body = '';
req.on('data', chunk => body += chunk);
req.on('end', () => {
console.log(`Forwarding to backend: ${path}`);
const options = {
hostname: 'localhost',
port: 3001,
path: path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body)
}
};
const proxyReq = http.request(options, proxyRes => {
let data = '';
proxyRes.on('data', chunk => data += chunk);
proxyRes.on('end', () => {
console.log(`Got response from backend for ${path}`);
res.writeHead(proxyRes.statusCode, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
});
res.end(data);
});
});
proxyReq.on('error', err => {
console.error('Proxy error:', err);
res.writeHead(500, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
});
res.end(JSON.stringify({error: err.message}));
});
proxyReq.write(body);
proxyReq.end();
});
}
const server = http.createServer((req, res) => {
console.log(`${req.method} ${req.url}`);
// CORS headers for ALL responses
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
// Handle preflight
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}
// Route POST requests to backend
if (req.method === 'POST') {
if (req.url === '/api/analyze-business') {
proxyToBackend(req, res, '/api/analyze-business');
return;
}
if (req.url === '/api/generate-foray') {
proxyToBackend(req, res, '/api/generate-foray');
return;
}
if (req.url === '/api/describe-transaction') {
proxyToBackend(req, res, '/api/describe-transaction');
return;
}
}
// Health check
if (req.method === 'GET' && req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'ok', proxy: true }));
return;
}
// Not found
res.writeHead(404);
res.end('Not found');
});
server.listen(3002, () => {
console.log('Proxy running on http://localhost:3002');
console.log('Forwarding /api/analyze-business -> localhost:3001');
console.log('Forwarding /api/generate-foray -> localhost:3001');
console.log('Forwarding /api/describe-transaction -> localhost:3001');
});