feat: Dynamische Server-URL und Parameter für API-Proxy hinzugefügt

- Dynamische Ermittlung von Hostname und Port aus der Anfrage (req.headers.host).
- Unterstützung für URL-Parameter 'm' (idMap) und 'u' (idUser) hinzugefügt.
- Fehlerbehandlung und Logging verbessert.
- CORS-Header und OPTIONS-Preflight für Cross-Origin-Anfragen konfiguriert.
- XML-Daten vom Webservice werden direkt an den Client weitergeleitet.
This commit is contained in:
ISA
2025-01-02 14:30:00 +01:00
parent 20a2abd9b6
commit 1625fbff12

View File

@@ -1,4 +1,3 @@
// funktioniert als Proxy , API-Proxy oder Server-side Proxy
export default async function handler(req, res) {
// CORS-Header setzen
res.setHeader("Access-Control-Allow-Credentials", true);
@@ -14,17 +13,25 @@ export default async function handler(req, res) {
try {
// Parameter aus URL oder Fallback-Werte verwenden
const idMap = req.query.m; // 'm' = idMap
const idUser = req.query.u; // 'u' = idUser
const idMap = req.query.m || 12; // 'm' = idMap
const idUser = req.query.u || 484; // 'u' = idUser
console.log("idMap:", idMap);
console.log("idUser:", idUser);
// API-URL für den Webservice
const url = `http://10.10.0.70/talas5/ClientData/WebServiceMap.asmx/GisStationsStaticDistrict?idMap=${idMap}&idUser=${idUser}`;
// Dynamische URL basierend auf der Anfrage
const protocol = req.headers["x-forwarded-proto"] || "http"; // Protokoll ermitteln
const host = req.headers.host.split(":")[0]; // Hostname ohne Port
const serverBaseUrl = `${protocol}://${host}:80`; // Port explizit auf 80 setzen
console.log("Dynamische Server-URL:", serverBaseUrl); // Debugging
// Ziel-URL für den Webservice
const targetUrl = `${serverBaseUrl}/talas5/ClientData/WebServiceMap.asmx/GisStationsStaticDistrict?idMap=${idMap}&idUser=${idUser}`;
console.log("Ziel-URL:", targetUrl); // Debugging
// Daten vom Webservice abrufen
const response = await fetch(url, {
const response = await fetch(targetUrl, {
method: "GET", // GET-Request
headers: {
"Content-Type": "application/xml", // XML als Antwort erwartet
@@ -38,7 +45,7 @@ export default async function handler(req, res) {
// XML-Antwort als Text auslesen
const xmlText = await response.text();
//console.log("XML-Antwort:", xmlText); // Debugging
console.log("XML-Antwort:", xmlText); // Debugging
// XML direkt an den Client zurückgeben
res.status(200).send(xmlText);