1

My angular project is running at http://127.0.0.1:4200 From http://127.0.0.1:4200/login I make a request to the api running at http://127.0.0.1:8000/api/register but I receive error 404 because of the wrong url requested: "http://127.0.0.1:4200/127.0.0.1:8000/api/register". So Angular proxy sends the request to http://127.0.0.1:4200/127.0.0.1:8000/api/register instead of http://127.0.0.1:8000/api/register.

proxy.conf.json is configured as fallows:

{
    "/api/register/*": {
    "target": "http://localhost:8000",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true
    }
}

environment.prod.ts:

export const environment = {
    production: true,
    API_BASE_PATH: 'http://127.0.0.1:8000/api'
};

and environment.ts:

export const environment = {
    production: false,
    API_BASE_PATH: '127.0.0.1:8000/api'
};

Any ideas how to solve this? Thank you!

1
  • Can you share the service file or the other file from where you are making HTTP get or post request to the API? Commented Nov 14, 2019 at 8:56

1 Answer 1

0

if your using proxy.conf.json for development purpose

you have to change it like this

{
"/register/*": {
"target": "http://localhost:8000/api",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}

and change

environment.ts:

export const environment = {
production: false,
API_BASE_PATH: ''
};

proxy config do the rest

Sign up to request clarification or add additional context in comments.

4 Comments

One mor question, on the server where I use a real proxy nginx I have to change the API_BASE_PATH: ' to the real base path on the server, like localhost:8000/api? and delete the proxy.conf.json file or at least deactivate it ?
yes, in server proxy config will not work, proxy config is developer hack, it only use in development mode, server deployment no need to worry about proxy.config
Cheers ..!! ,have a nice day !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.