我使用spring mvc
工作在端口8080和angular 5
所以我尝试这种配置,以便4200的客户端将代理任何/ api请求到服务器。
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 url http://127.0.0.1:8080/NG-PROJETS/new-projet
工作正常。
proxy.conf.json
{
"/api": {
"target": "http://localhost:8080/NG-PROJETS",
"secure": false,
"pathRewrite": {
"^/api": ""
}
}
}