| const express = require("express"); |
| const cors = require("cors"); |
| const compression = require("compression"); |
| const path = require("path"); |
| const serveStatic = require("serve-static"); |
| const { createProxyMiddleware } = require("http-proxy-middleware"); |
|
|
| const app = express(); |
| const port = process.env.PORT || 7860; |
| const apiPort = process.env.INTERNAL_API_PORT || 7861; |
|
|
| |
| app.use(cors()); |
|
|
| |
| app.use(compression()); |
|
|
| |
| app.use( |
| "/api", |
| createProxyMiddleware({ |
| target: `http://127.0.0.1:${apiPort}`, |
| changeOrigin: true, |
| onError: (err, req, res) => { |
| console.error("Proxy Error:", err); |
| res.status(500).json({ error: "Proxy Error", details: err.message }); |
| }, |
| }) |
| ); |
|
|
| |
| app.use( |
| express.static(path.join(__dirname, "build"), { |
| |
| setHeaders: (res, path) => { |
| if (path.endsWith(".html")) { |
| res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); |
| res.setHeader("Pragma", "no-cache"); |
| res.setHeader("Expires", "0"); |
| } else { |
| |
| res.setHeader("Cache-Control", "public, max-age=31536000"); |
| } |
| }, |
| }) |
| ); |
|
|
| |
| app.use((req, res, next) => { |
| |
| if (req.url.startsWith("/api")) { |
| return next(); |
| } |
|
|
| |
| req.originalUrl = req.url; |
| next(); |
| }); |
|
|
| |
| app.get("*", (req, res) => { |
| |
| if (req.url.startsWith("/api")) { |
| return next(); |
| } |
|
|
| |
| res.set({ |
| "Cache-Control": "no-cache, no-store, must-revalidate", |
| Pragma: "no-cache", |
| Expires: "0", |
| }); |
|
|
| |
| res.sendFile(path.join(__dirname, "build", "index.html")); |
| }); |
|
|
| app.listen(port, "0.0.0.0", () => { |
| console.log( |
| `Frontend server is running on port ${port} in ${ |
| process.env.NODE_ENV || "development" |
| } mode` |
| ); |
| console.log(`API proxy target: http://127.0.0.1:${apiPort}`); |
| }); |
|
|