角度代理无法正常工作

时间:2017-12-18 11:02:59

标签: angular spring-mvc

我使用spring mvc在端口 8080 angular 5上工作,所以我尝试这种配置,以便4200处的客户端将代理服务器的任何/ api请求。< / p>

proxy.conf.json

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

我编辑“start”脚本的package.json文件:

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

但是当我尝试

  ngOnInit() {

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

我运行角度服务器:

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.

我在Chrome控制台中遇到此错误 GET http://localhost:4200/api/new-projet 404 (Not Found)
那么问题出在哪里?

P.S网址http://127.0.0.1:8080/NG-PROJETS/new-projet正常工作。

1 个答案:

答案 0 :(得分:1)

proxy.conf.json

var task = await Task.WhenAny(_tasks.Values);
var id = _tasks.Single(pair => pair.Value == task).Key;