🚧 replace host header to fix proxy

This commit is contained in:
Nate Sesti 2023-12-10 16:31:10 -08:00
parent 65bd09b49a
commit 975c10f28f

View file

@ -8,24 +8,18 @@ const app = express();
app.use(cors());
app.use((req, res, next) => {
// Don't proxy the /sw route
if (req.path === "/sw" || req.path === "/service-worker.js") {
return next();
}
if (!req.headers["x-continue-url"]) {
// Just let the request through
return next();
}
// Proxy the request
const { origin, ...headers } = req.headers;
const { origin, host, ...headers } = req.headers;
const url = req.headers["x-continue-url"] as string;
const parsedUrl = new URL(url);
const protocolString = url.split("://")[0];
const protocol = protocolString === "https" ? https : http;
const proxy = protocol.request(url, {
method: req.method,
headers: headers,
headers: {
...headers,
host: parsedUrl.host,
},
});
proxy.on("response", (response) => {
@ -59,29 +53,6 @@ app.use((req, res, next) => {
// proxy(req, res, next);
// });
app.get("/sw", (req, res) => {
// Send a script that will register the service worker
res.send(
`<html><body><script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js')
.then(function(registration) {
console.log('Service Worker registered with scope:', registration.scope);
})
.catch(function(error) {
console.log('Service Worker registration failed:', error);
});
}
</script></body></html>`
);
});
app.get("/service-worker.js", (req, res) => {
// Send the service worker script
res.sendFile("/service-worker.js", { root: __dirname });
});
export function startProxy() {
app.listen(PROXY_PORT, () => {
console.log(`Proxy server is running on port ${PROXY_PORT}`);