-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy-server.mjs
More file actions
54 lines (45 loc) · 1.53 KB
/
Copy pathproxy-server.mjs
File metadata and controls
54 lines (45 loc) · 1.53 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
import http from 'http';
const PORT = 7777;
const server = http.createServer(async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
if (req.method === 'GET' && req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ ok: true }));
return;
}
if (req.method !== 'POST') {
res.writeHead(405);
res.end('Method not allowed');
return;
}
let raw = '';
for await (const chunk of req) raw += chunk;
try {
const { url, method, headers, body } = JSON.parse(raw);
const upstream = await fetch(url, { method, headers, body: body ?? undefined });
const text = await upstream.text();
const responseHeaders = {};
upstream.headers.forEach((value, key) => { responseHeaders[key] = value; });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
status: upstream.status,
statusText: upstream.statusText,
headers: responseHeaders,
body: text,
}));
} catch (e) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: String(e) }));
}
});
server.listen(PORT, () => {
console.log(`Local proxy running → http://localhost:${PORT}`);
console.log('Keep this terminal open while using API Client.\n');
});