Files
Lukas Cremer 9576e9cf44 init commit
2026-05-16 15:57:49 +02:00

66 lines
1.8 KiB
JavaScript

import http from 'http';
import https from 'https';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PORT = 3000;
const MIME = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
};
function proxyOnvista(req, res) {
const url = new URL(req.url, `http://localhost:${PORT}`);
// Forward all query params as-is to onvista
const upstream = `https://api.onvista.de/api/v1/bonds/finder/configuration_query?${url.searchParams.toString()}`;
const options = {
headers: {
'User-Agent': 'Mozilla/5.0',
'Accept': 'application/json',
}
};
https.get(upstream, options, upRes => {
let body = '';
upRes.on('data', chunk => body += chunk);
upRes.on('end', () => {
res.writeHead(upRes.statusCode, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
});
res.end(body);
});
}).on('error', err => {
res.writeHead(502, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: err.message }));
});
}
function serveStatic(req, res) {
const filePath = path.join(__dirname, 'static', req.url === '/' ? 'index.html' : req.url);
const ext = path.extname(filePath);
fs.readFile(filePath, (err, data) => {
if (err) { res.writeHead(404); res.end('Not found'); return; }
res.writeHead(200, { 'Content-Type': MIME[ext] || 'text/plain' });
res.end(data);
});
}
const server = http.createServer((req, res) => {
if (req.url.startsWith('/api/bonds')) {
proxyOnvista(req, res);
} else {
serveStatic(req, res);
}
});
server.listen(PORT, () => console.log(`Bond Sniffer running at http://localhost:${PORT}`));