I am trying to call an API locally in my angular 6 app, the API URL is http://localhost/myapi
I added a proxy config file that point to this URL but I got
404
error when I run my app.
My proxy.config.json file:
{
"/api/*":{
"target":"http://localhost/myapi",
"secure": false,
"logLevel": "debug"
}
}
package.json file:
"serve-dev": "ng serve --source-map=false --proxy-config proxy.config.json",
http.service.ts file:
export class HttpService{
static serverApiUrl : string = "/api/"
}
auth.service.ts file :
this.http.get(HttpService.serverApiUrl+"site/check-auth")
.map(response => {
if(response['auth'] == 1) {
return true;
} else {
this.router.navigate(['/auth'])
return false;
}
});
In the console I got this:
http://localhost:4200/api/site/check-auth
any suggestions.
I am trying to call an API locally in my angular 6 app, the API URL is http://localhost/myapi
I added a proxy config file that point to this URL but I got
404
error when I run my app.
My proxy.config.json file:
{
"/api/*":{
"target":"http://localhost/myapi",
"secure": false,
"logLevel": "debug"
}
}
package.json file:
"serve-dev": "ng serve --source-map=false --proxy-config proxy.config.json",
http.service.ts file:
export class HttpService{
static serverApiUrl : string = "/api/"
}
auth.service.ts file :
this.http.get(HttpService.serverApiUrl+"site/check-auth")
.map(response => {
if(response['auth'] == 1) {
return true;
} else {
this.router.navigate(['/auth'])
return false;
}
});
In the console I got this:
http://localhost:4200/api/site/check-auth
any suggestions.
Share Improve this question edited Dec 28, 2023 at 17:02 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 26, 2018 at 7:31 Mhmd Backer ShehadiMhmd Backer Shehadi 5891 gold badge12 silver badges31 bronze badges 4-
Are you doing
ng serve
ornpm start
? – Amit Chigadani Commented Sep 26, 2018 at 7:40 - npm run serve-dev – Mhmd Backer Shehadi Commented Sep 26, 2018 at 7:44
- the app builds but the request return 404 error – Mhmd Backer Shehadi Commented Sep 26, 2018 at 7:45
- the real api url is localhost/myapi/site/check-auth – Mhmd Backer Shehadi Commented Sep 26, 2018 at 7:46
1 Answer
Reset to default 2It looks like you need to rewrite the URL path, using something like this:
{
"/api": {
"target": "http://localhost",
"secure": false,
"logLevel": "debug",
"pathRewrite": {
"^/api": "/myapi"
}
}
}
I've removed /*
from the prefix you had and added a pathRewrite
section. I believe what you had before was attempting to proxy to localhost/myapi/api/site/check-auth
(with that extra /api
), which should be stripped with the pathRewrite
section.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745314319a4622143.html
评论列表(0条)