Spaces:
Configuration error
Configuration error
| const express = require("express"); | |
| const cors = require("cors"); | |
| const axios = require("axios"); | |
| const helmet = require("helmet"); | |
| const path = require("path"); | |
| const app = express(); | |
| const PORT = process.env.PORT || 3001; | |
| // Security headers | |
| app.use(helmet()); | |
| // CORS (restrict in production) | |
| app.use( | |
| cors({ | |
| origin: process.env.CLIENT_URL || "*", | |
| methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], | |
| allowedHeaders: ["Content-Type", "Authorization"], | |
| }) | |
| ); | |
| app.use(express.json({ limit: "50mb" })); | |
| app.use(express.urlencoded({ extended: true, limit: "50mb" })); | |
| // ============================= | |
| // Serve Frontend (React/Vite) | |
| // ============================= | |
| app.use( | |
| express.static(path.join(__dirname, "dist"), { | |
| setHeaders: (res, filePath) => { | |
| if (filePath.endsWith(".js") || filePath.endsWith(".css")) { | |
| // Cache hashed assets for 1 year | |
| res.setHeader("Cache-Control", "public, max-age=31536000, immutable"); | |
| } else { | |
| // Cache other assets (images, etc.) for 1 hour | |
| res.setHeader("Cache-Control", "public, max-age=3600"); | |
| } | |
| }, | |
| }) | |
| ); | |
| // SPA fallback β always return index.html | |
| app.get("*", (req, res, next) => { | |
| if (req.path.startsWith("/api")) return next(); // skip API routes | |
| res.sendFile(path.join(__dirname, "dist", "index.html")); | |
| }); | |
| // ============================= | |
| // Proxy /api requests | |
| // ============================= | |
| app.use("/api", async (req, res, next) => { | |
| try { | |
| const url = `https://humanizerx.pro/api/routes.php${req.originalUrl.replace( | |
| "/api", | |
| "" | |
| )}`; | |
| const method = req.method.toLowerCase(); | |
| const { data, status } = await axios({ | |
| url, | |
| method, | |
| headers: { | |
| "Content-Type": req.headers["content-type"], | |
| Authorization: req.headers["authorization"], | |
| }, | |
| data: req.body, | |
| }); | |
| res.status(status).json(data); | |
| } catch (error) { | |
| console.error("API Proxy Error:", error.message); | |
| res.status(error.response?.status || 500).json({ | |
| success: false, | |
| error: error.message, | |
| }); | |
| } | |
| }); | |
| // Test route | |
| app.get("/test", (req, res) => { | |
| res.json({ msg: "Hello from Humanizer X backend π" }); | |
| }); | |
| app.listen(PORT, () => { | |
| console.log(`β Humanizer X Backend running on http://localhost:${PORT}`); | |
| }); | |