-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
53 lines (46 loc) · 1.49 KB
/
Copy pathserver.js
File metadata and controls
53 lines (46 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const express = require("express");
const path = require("path");
const app = express();
const PORT = process.env.PORT || 3000;
const distDir = path.join(__dirname, "dist");
// CORS headers for RTE plugin (runs cross-origin from app.contentstack.com)
app.use("/json-rte.js", (req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
next();
});
app.use("/api/lytics", (req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type");
if (req.method === "OPTIONS") {
return res.sendStatus(204);
}
next();
});
// Proxy for Lytics API (avoids browser CORS restrictions)
app.get("/api/lytics/schema", async (req, res) => {
const token = req.headers.authorization;
if (!token) {
return res.status(400).json({ error: "Authorization header required" });
}
try {
const response = await fetch("https://api.lytics.io/api/schema/user", {
headers: {
Authorization: token,
Accept: "application/json",
},
});
const data = await response.json();
res.status(response.status).json(data);
} catch (err) {
res.status(502).json({ error: "Failed to reach Lytics API" });
}
});
// Serve static files from dist
app.use(express.static(distDir));
// SPA fallback — serve index.html for all other routes
app.get("*", (req, res) => {
res.sendFile(path.join(distDir, "index.html"));
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});