-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.mjs
More file actions
37 lines (31 loc) · 1.12 KB
/
proxy.mjs
File metadata and controls
37 lines (31 loc) · 1.12 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
// Lightweight reverse proxy for AI-CMS
// Proxies requests from a free port to Laravel on 8000
import { createServer, request } from 'http';
const TARGET_HOST = '127.0.0.1';
const TARGET_PORT = 8000;
const PROXY_PORT = parseInt(process.env.PROXY_PORT || '18790');
const server = createServer((clientReq, clientRes) => {
const options = {
hostname: TARGET_HOST,
port: TARGET_PORT,
path: clientReq.url,
method: clientReq.method,
headers: {
...clientReq.headers,
host: `${TARGET_HOST}:${TARGET_PORT}`,
},
};
const proxy = request(options, (proxyRes) => {
clientRes.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(clientRes, { end: true });
});
proxy.on('error', (err) => {
console.error('Proxy error:', err.message);
clientRes.writeHead(502);
clientRes.end('Bad Gateway');
});
clientReq.pipe(proxy, { end: true });
});
server.listen(PROXY_PORT, '0.0.0.0', () => {
console.log(`AI-CMS proxy running on http://0.0.0.0:${PROXY_PORT} → http://${TARGET_HOST}:${TARGET_PORT}`);
});