Hayi
Hayi

Reputation: 6236

Angular proxy not working

I use spring mvc which work on port 8080 and angular 5 so i try this configuration so that the client at 4200 will proxy any /api requests to the server.

proxy.conf.json

{
    "/api": {
        "target": "http://localhost:8080/NG-PROJETS",
        "secure": false
    }
}

I Edit package.json file for “start” script:

 "scripts": {
    "ng": "ng",
    "start": "ng serve --proxy-config proxy.conf.json",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"   
 }

But when i try

  ngOnInit() {

    this.http.get('/api/new-projet').subscribe(data => {
      console.log(data);
    });
  }

I run angular server:

PS C:\Users\Yous\Desktop\NG-DEV\projets> ng serve
** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Date: 2017-12-18T11:27:05.809Z
Hash: b7bab060a44cd3aad50d
Time: 6395ms
chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] [rendered]
chunk {main} main.bundle.js (main) 49.3 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 550 kB [initial] [rendered]
chunk {styles} styles.bundle.js (styles) 34.5 kB [initial] [rendered]
chunk {vendor} vendor.bundle.js (vendor) 7.47 MB [initial] [rendered]

webpack: Compiled successfully.

I get this error in the chrome console
GET http://localhost:4200/api/new-projet 404 (Not Found)
So where is the problem ?

P.S the url http://127.0.0.1:8080/NG-PROJETS/new-projet works fine.

Upvotes: 0

Views: 795

Answers (1)

anode7
anode7

Reputation: 396

proxy.conf.json

{
  "/api": {
    "target": "http://localhost:8080/NG-PROJETS",
    "secure": false,
    "pathRewrite": {
      "^/api": ""
    }
  }
}

Upvotes: 2

Related Questions