mirror of
				https://github.com/9technologygroup/patchmon.net.git
				synced 2025-10-31 20:13:50 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			51 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| import cors from "cors";
 | |
| import express from "express";
 | |
| import { createProxyMiddleware } from "http-proxy-middleware";
 | |
| import path from "path";
 | |
| import { fileURLToPath } from "url";
 | |
| 
 | |
| const __filename = fileURLToPath(import.meta.url);
 | |
| const __dirname = path.dirname(__filename);
 | |
| 
 | |
| const app = express();
 | |
| const PORT = process.env.PORT || 3000;
 | |
| const BACKEND_URL = process.env.BACKEND_URL || "http://backend:3001";
 | |
| 
 | |
| // Enable CORS for API calls
 | |
| app.use(
 | |
| 	cors({
 | |
| 		origin: process.env.CORS_ORIGIN || "*",
 | |
| 		credentials: true,
 | |
| 	}),
 | |
| );
 | |
| 
 | |
| // Proxy API requests to backend
 | |
| app.use(
 | |
| 	"/api",
 | |
| 	createProxyMiddleware({
 | |
| 		target: BACKEND_URL,
 | |
| 		changeOrigin: true,
 | |
| 		logLevel: "info",
 | |
| 		onError: (err, req, res) => {
 | |
| 			console.error("Proxy error:", err.message);
 | |
| 			res.status(500).json({ error: "Backend service unavailable" });
 | |
| 		},
 | |
| 		onProxyReq: (proxyReq, req, res) => {
 | |
| 			console.log(`Proxying ${req.method} ${req.path} to ${BACKEND_URL}`);
 | |
| 		},
 | |
| 	}),
 | |
| );
 | |
| 
 | |
| // Serve static files from dist directory
 | |
| app.use(express.static(path.join(__dirname, "dist")));
 | |
| 
 | |
| // Handle SPA routing - serve index.html for all routes
 | |
| app.get("*", (req, res) => {
 | |
| 	res.sendFile(path.join(__dirname, "dist", "index.html"));
 | |
| });
 | |
| 
 | |
| app.listen(PORT, () => {
 | |
| 	console.log(`Frontend server running on port ${PORT}`);
 | |
| 	console.log(`Serving from: ${path.join(__dirname, "dist")}`);
 | |
| });
 |