Angular 'ng serve' API-Proxy konfigurieren

English Deutsch

Um z.B. /api auf http://localhost:62232 zu proxen, erstelle zuerst proxy.conf.json im selben Verzeichnis, in dem sich package.json befindet:

proxy.conf.json
{
    "/api":
    {
        "target": "http://localhost:62232",
        "secure": false
    }
}

Jetzt müssen wir package.json ändern. Finde die Zeile, in der ng serve aufgerufen wird, wie z.B.:

package.json
"start": "ng serve",

und füge --proxy-config proxy.conf.json zu den Argumenten von ng serve hinzu:

package.json
"start": "ng serve --proxy-config proxy.conf.json",

Vollständiges Beispiel für den scripts-Abschnitt von package.json:

package.json
"scripts": {
  "ng": "ng",
  "start": "ng serve --proxy-config proxy.conf.json",
  "build": "ng build --configuration=production",
  "watch": "ng build --watch --configuration development",
  "test": "ng test"
},

Check out similar posts by category: Angular, Javascript