ps2_artwork_api/api.js
2025-04-22 18:48:37 +02:00

49 lines
1.3 KiB
JavaScript

const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = 3000;
const data = fs.readFileSync('db/PS2_LIST.txt', 'utf-8');
const gameMap = {};
data.split('\n').forEach(line => {
const match = line.match(/^(\S+)\s+(.*)$/);
if (match) {
const [_, id, title] = match;
gameMap[id] = title.trim();
}
});
function normalizeId(input) {
const cleaned = input.toUpperCase().replace(/[_\.]/g, ''); // remove _ and .
const match = cleaned.match(/^([A-Z]+)(\d+)$/);
if (!match) return input.toUpperCase(); // fallback
const [, prefix, digits] = match;
return `${prefix}-${digits}`;
}
app.get('/search/:id', (req, res) => {
const normalizedId = normalizeId(req.params.id);
const title = gameMap[normalizedId];
if (title) {
res.json({ id: normalizedId, title });
} else {
res.status(404).json({ error: 'Game ID not found' });
}
});
app.use('/art', express.static(path.join(__dirname, 'db/PS2')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/contact', (req, res) => {
res.sendFile(path.join(__dirname, 'contact.html'));
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});