Something went wrong. Try again.
An open-source, container-based Platform-as-a-Service (PAAS) for seamless deployments and effortless infrastructure management — built on f…
Something went wrong. Try again.
1.3 kB · 46 lines
TypeScript
at devops
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
import dotenv from 'dotenv';import express, { Request, Response } from 'express';import { ClientRequest, IncomingMessage } from 'http';import httpProxy from 'http-proxy';
dotenv.config();
const app = express();const proxy = httpProxy.createProxyServer();
// Function to resolve target URL based on subdomainconst resolveTarget = (hostname: string): string => { const subdomain = hostname.split('.')[0]; return `${process.env.BASE_PATH}/${subdomain}`;};
// Middleware to handle proxyingapp.use((req: Request, res: Response) => { const target = resolveTarget(req.hostname); proxy.web(req, res, { target, changeOrigin: true }, (err) => { if (err) { console.error('Proxy error:', err); res.status(500).send('Proxy error'); } });});
// Modify proxy request path if neededproxy.on('proxyReq', (proxyReq: ClientRequest, req: IncomingMessage) => { const host = req.headers.host; if (host) { const target = resolveTarget(host); if (!req.url?.includes('.')) { proxyReq.path = `${target}/index.html`; } } else { console.error('Host header is missing'); }});
// Start the serverconst port = process.env.PORT;app.listen(port, () => { console.log(`--- Reverse Proxy listening on port ${port}`);});