我试图在我的Range6应用程序中本地调用一个api,api url是http://localhost/myapi --我添加了一个指向这个url的代理配置文件,但是当我运行我的应用程序时,我得到了404错误。
我的proxy.config.json文件:
{
"/api/*":{
"target":"http://localhost/myapi",
"secure": false,
"logLevel": "debug"
}
}package.json文件:
"serve-dev": "ng serve --source-map=false --proxy-config proxy.config.json",http.service.ts文件:
export class HttpService{
static serverApiUrl : string = "/api/"
}auth.service.ts文件:
this.http.get(HttpService.serverApiUrl+"site/check-auth")
.map(response => {
if(response['auth'] == 1) {
return true;
} else {
this.router.navigate(['/auth'])
return false;
}
});在控制台里我得到了这样的信息:
http://localhost:4200/api/site/check-auth 任何建议。
发布于 2018-09-26 07:50:17
看起来您需要使用类似于以下内容的重写URL路径:
{
"/api": {
"target": "http://localhost",
"secure": false,
"logLevel": "debug",
"pathRewrite": {
"^/api": "/myapi"
}
}
}我从前缀中删除了/*,并添加了一个pathRewrite部分。我相信您之前所做的是尝试代理到localhost/myapi/api/site/check-auth (使用额外的/api),应该使用pathRewrite部分来删除它。
https://stackoverflow.com/questions/52512301
复制相似问题