Files
whattoplay/server/index.js
Felix Förtsch 6a6fe467e1 fix API routing: remove /api prefix in Express routes
Uberspace web backend removes /api prefix before forwarding to Express,
so routes must not include /api. Changed from app.all('/api/*') to
app.all('/*').

/health route is defined first, so it takes precedence.

Fixes 404 errors when calling Steam API from production.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-06 21:50:51 +01:00

62 lines
1.4 KiB
JavaScript

import express from "express";
import cors from "cors";
import fetch from "node-fetch";
const app = express();
const PORT = process.env.PORT || 3000;
// Enable CORS for your PWA
app.use(
cors({
origin: process.env.ALLOWED_ORIGIN || "*",
}),
);
app.use(express.json());
// Health check
app.get("/health", (req, res) => {
res.json({ status: "ok" });
});
// Proxy for Steam API
// Note: Uberspace removes /api prefix, so we get /<path> here
app.all("/*", async (req, res) => {
const path = req.url;
const steamUrl = `https://store.steampowered.com${path}`;
console.log(`Proxying: ${req.method} ${steamUrl}`);
try {
const response = await fetch(steamUrl, {
method: req.method,
headers: {
"User-Agent": "WhatToPlay/1.0",
Accept: "application/json",
...(req.body && { "Content-Type": "application/json" }),
},
...(req.body && { body: JSON.stringify(req.body) }),
});
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
const data = await response.json();
res.json(data);
} else {
const text = await response.text();
res.send(text);
}
} catch (error) {
console.error("Proxy error:", error);
res.status(500).json({
error: "Proxy error",
message: error.message,
});
}
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});