Files
whattoplay/server/index.js
Felix Förtsch cfe6384f75 implement proper Steam API endpoint for /steam/refresh
Fixed issue where backend was forwarding /steam/refresh to
store.steampowered.com instead of calling the actual Steam Web API.

Now properly calls:
https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/

With parameters from request body (apiKey, steamId).

Fixes 'the string did not match the expected pattern' error.

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

97 lines
2.2 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" });
});
// Steam API refresh endpoint
app.post("/steam/refresh", async (req, res) => {
const { apiKey, steamId } = req.body;
console.log(`Steam refresh for user: ${steamId}`);
if (!apiKey || !steamId) {
return res.status(400).json({
error: "Missing required fields: apiKey and steamId",
});
}
try {
// Call Steam Web API
const steamUrl = `https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/?key=${apiKey}&steamid=${steamId}&include_appinfo=1&include_played_free_games=1&format=json`;
const response = await fetch(steamUrl);
if (!response.ok) {
return res.status(response.status).json({
error: "Steam API error",
message: response.statusText,
});
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error("Steam API error:", error);
res.status(500).json({
error: "Failed to fetch games",
message: error.message,
});
}
});
// Fallback proxy for other Steam API calls
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}`);
});