diff --git a/CHANGELOG.md b/CHANGELOG.md index 72660352c..fb5d03057 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,22 @@ Alle bedeutenden Änderungen an diesem Projekt werden in dieser Datei dokumentie --- +## [1.1.77] – 2025-05-17 + +### Changed + +- `pages/api/[...path].js`: + - feste Umgebungsvariable `NEXT_PUBLIC_SERVER_URL` entfernt + - dynamische Proxy-Zieladresse per `NEXT_PUBLIC_API_PORT_MODE` definiert (dev vs. prod) + - Konfiguration via `http-proxy-middleware` vereinfacht + +### Added + +- Dokumentation erstellt: + - `docs/server/pages/api/apiProxy.md` + +--- + ## [1.1.76] – 2025-05-17 ### Changed diff --git a/README.md b/README.md index b9a145672..7b0a724c6 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,6 @@ Beispiel: ```env NEXT_PUBLIC_APP_VERSION=1.1.59 NEXT_PUBLIC_USE_MOCK_API=true -NEXT_PUBLIC_SERVER_URL=http://10.10.0.13:3000 ``` - Nur `NEXT_PUBLIC_...` ist im Browser sichtbar @@ -237,7 +236,6 @@ NEXT_PUBLIC_APP_VERSION=1.1.59 ```env NEXT_PUBLIC_APP_VERSION=1.1.59 NEXT_PUBLIC_USE_MOCK_API=true -NEXT_PUBLIC_SERVER_URL=http://10.10.0.13:3000 ``` --- diff --git a/config/appVersion.js b/config/appVersion.js index 711bafd18..d0722fd76 100644 --- a/config/appVersion.js +++ b/config/appVersion.js @@ -1,2 +1,2 @@ // /config/appVersion -export const APP_VERSION = "1.1.77"; +export const APP_VERSION = "1.1.78"; diff --git a/config/config.js b/config/config.js index 4d3a6f8c9..8ab06cca1 100644 --- a/config/config.js +++ b/config/config.js @@ -12,7 +12,7 @@ const mode = process.env.NEXT_PUBLIC_API_PORT_MODE; const serverURL = mode === "dev" ? `${window.location.protocol}//${window.location.hostname}:80` : `${window.location.origin}`; if (!serverURL && !isMockMode()) { - throw new Error("Die Umgebungsvariable NEXT_PUBLIC_SERVER_URL ist nicht gesetzt!"); + throw new Error("Die Umgebungsvariable ist nicht gesetzt!"); } console.log("%c 1- serverURL in config:", "color: #006400;", serverURL); diff --git a/docs/server/pages/api/apiProxy.md b/docs/server/pages/api/apiProxy.md new file mode 100644 index 000000000..2fd6741af --- /dev/null +++ b/docs/server/pages/api/apiProxy.md @@ -0,0 +1,77 @@ +# 🌐 API Proxy – [...path].js + +## Zweck + +Diese Datei (`pages/api/[...path].js`) dient als **Catch-All Proxy** +für externe Webservice-Aufrufe über die Next.js API-Routing-Struktur. + +Sie leitet alle Anfragen unter `/api/...` an einen Zielserver weiter. + +--- + +## Technologie + +Verwendet wird: + +- [`http-proxy-middleware`](https://github.com/chimurai/http-proxy-middleware) +- Dynamische Zielauswahl basierend auf Umgebungsvariable `NEXT_PUBLIC_API_PORT_MODE` + +--- + +## Ziel-URL-Konfiguration + +```js +const mode = process.env.NEXT_PUBLIC_API_PORT_MODE; + +const target = + mode === "dev" + ? "http://localhost:80" + : "http://localhost"; // oder z. B. http://10.10.0.13 +``` + +➡ Dadurch entfällt die feste Konfiguration über `NEXT_PUBLIC_SERVER_URL`. + +--- + +## Beispiel: Weiterleitung + +Request im Browser: + +``` +GET /api/talas5/ClientData/WebServiceMap.asmx/GisSystemStatic?idMap=12&idUser=484 +``` + +→ wird weitergeleitet an: + +``` +http://localhost/talas5/ClientData/WebServiceMap.asmx/GisSystemStatic?idMap=12&idUser=484 +``` + +--- + +## Verwendete Einstellungen + +```js +export default createProxyMiddleware({ + target, + changeOrigin: true, + pathRewrite: { + "^/api": "/", // entfernt /api aus dem Pfad + }, + logLevel: "debug", +}); +``` + +--- + +## Vorteile + +| Punkt | Nutzen | +|---------------------------|------------------------------------------| +| Keine doppelte API-Konfiguration | ✅ `.env.local` bereinigt | +| Wiederverwendbar & flexibel | ✅ funktioniert lokal & auf Servern | +| Klar gekapselt in `[...path].js` | ✅ übersichtlich | + +--- + +📄 Pfad: `/docs/server/pages/api/apiProxy.md` \ No newline at end of file diff --git a/pages/api/[...path].js b/pages/api/[...path].js index d8b241e43..4625493a6 100644 --- a/pages/api/[...path].js +++ b/pages/api/[...path].js @@ -1,13 +1,15 @@ // pages/api/[...path].js import { createProxyMiddleware } from "http-proxy-middleware"; -//import { SERVER_URL } from "../config/urls.js"; -//console.log("SERVER_URL:", SERVER_URL); // Debug-Ausgabe + +const mode = process.env.NEXT_PUBLIC_API_PORT_MODE; + +const target = mode === "dev" ? "http://localhost:80" : "http://localhost"; // oder z. B. http://10.10.0.13 export default createProxyMiddleware({ - target: `${process.env.NEXT_PUBLIC_SERVER_URL}`, // + target, changeOrigin: true, pathRewrite: { - "^/api": "/", // Optional: Entfernt /api aus dem Pfad, wenn das Backend dies nicht erfordert + "^/api": "/", // Entfernt /api aus dem Pfad, wenn das Backend das nicht erwartet }, - logLevel: "debug", // Setzt das Logging-Level auf "debug" für detaillierte Ausgaben + logLevel: "debug", });