docs+refactor: Proxy [...path].js auf dynamische Ziel-URL umgestellt (v1.1.77)

- entfernt: NEXT_PUBLIC_SERVER_URL aus .env.local
- verwendet jetzt API_PORT_MODE zur Zielermittlung (dev = :80)
- neue technische Dokumentation unter /docs/server/pages/api/apiProxy.md
- CHANGELOG.md und appVersion.js aktualisiert (1.1.77)
This commit is contained in:
Ismail Ali
2025-05-17 01:10:18 +02:00
parent 30bbb61f1c
commit b097a76d34
6 changed files with 102 additions and 9 deletions

View File

@@ -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 ## [1.1.76] 2025-05-17
### Changed ### Changed

View File

@@ -159,7 +159,6 @@ Beispiel:
```env ```env
NEXT_PUBLIC_APP_VERSION=1.1.59 NEXT_PUBLIC_APP_VERSION=1.1.59
NEXT_PUBLIC_USE_MOCK_API=true NEXT_PUBLIC_USE_MOCK_API=true
NEXT_PUBLIC_SERVER_URL=http://10.10.0.13:3000
``` ```
- Nur `NEXT_PUBLIC_...` ist im Browser sichtbar - Nur `NEXT_PUBLIC_...` ist im Browser sichtbar
@@ -237,7 +236,6 @@ NEXT_PUBLIC_APP_VERSION=1.1.59
```env ```env
NEXT_PUBLIC_APP_VERSION=1.1.59 NEXT_PUBLIC_APP_VERSION=1.1.59
NEXT_PUBLIC_USE_MOCK_API=true NEXT_PUBLIC_USE_MOCK_API=true
NEXT_PUBLIC_SERVER_URL=http://10.10.0.13:3000
``` ```
--- ---

View File

@@ -1,2 +1,2 @@
// /config/appVersion // /config/appVersion
export const APP_VERSION = "1.1.77"; export const APP_VERSION = "1.1.78";

View File

@@ -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}`; const serverURL = mode === "dev" ? `${window.location.protocol}//${window.location.hostname}:80` : `${window.location.origin}`;
if (!serverURL && !isMockMode()) { 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); console.log("%c 1- serverURL in config:", "color: #006400;", serverURL);

View File

@@ -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`

View File

@@ -1,13 +1,15 @@
// pages/api/[...path].js // pages/api/[...path].js
import { createProxyMiddleware } from "http-proxy-middleware"; 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({ export default createProxyMiddleware({
target: `${process.env.NEXT_PUBLIC_SERVER_URL}`, // target,
changeOrigin: true, changeOrigin: true,
pathRewrite: { 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",
}); });