-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproxy.js
More file actions
39 lines (34 loc) · 1.28 KB
/
proxy.js
File metadata and controls
39 lines (34 loc) · 1.28 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
// TODO: never quite got this to work
import http from "http";
import httpProxy from "http-proxy";
const proxy = httpProxy.createProxyServer();
http
.createServer(function (req, res) {
try {
// Parse the target URL from the query parameter, e.g. ?q=https://example.com
const urlParams = new URL(req.url, `http://${req.headers.host}`).searchParams;
const targetUrl = urlParams.get("q");
if (!targetUrl) {
res.writeHead(400);
return res.end('Missing target URL query parameter "q"');
}
// Remove query string from path and set it properly on proxy request
proxy.on("proxyReq", (proxyReq) => {
const parsedUrl = new URL(targetUrl);
proxyReq.path = parsedUrl.pathname + parsedUrl.search;
// Optionally, adjust headers if needed
proxyReq.setHeader("host", parsedUrl.host);
});
// Proxy the request to the target URL specified in 'q'
proxy.web(req, res, { target: targetUrl, changeOrigin: true }, (err) => {
res.writeHead(502);
res.end("Proxy error: " + err.message);
});
} catch (err) {
res.writeHead(500);
res.end("Server error: " + err.message);
}
})
.listen(8080, () => {
console.log("Proxy server running on http://localhost:8080");
});