I am looking to deploy my angular project. I currently tell it which api url to hit through a proxy.config.json { "/api/*": { "target": "http://localhost:3000", "secure": false }, "/__/*": { "target": "http://localhost:3100", "secure": false } }and i start up my app with ng serve --proxy-config proxy.config.json what do i need to do to make it point to my deployed API for when i deploy my application. i.e locally i want it to hit localhost but deployed i want it to hit my production api url.
#how to configure production url
22 messages · Page 1 of 1 (latest)
Generally you do that using environment.ts and enviornment.prod.ts
what do i need to do for example, environment.ts to tell it to hit localhost:3000?
i see, and when starting the app how will it know what environment im in?
just got it to work
though im running into this Access to XMLHttpRequest at 'http://localhost:3000/api/auth/login' from origin 'http://localhost:4200' has been blocked by CORS policy: which i wasnt before
--configuration flag. The default for ng build is "production" as defined in angular.json
Sounds like you have your configurations backwards
localhost 4200 is the frontend, and 3000 is the backend
What did you do?
i was using proxy.config.json before so i wasnt running into issues
i just spun it up with ng serve
and i am seeing this cors issue
You still need the proxy with localhost. Or better fix your API server to do CORS
im trying to the latter actually now
what are cors and why are they asking me for it?
is it to check the integrity of the request i.e where its coming from?
You understand that ng serve is not for production, right?
yes
g!cors @glass nest
@glass nest CORS is not an Angular specific issue. You will need to configure your API server to be able to accept cross-origin requests.
For development environment only
In case you do not have access to the API server, you can set up a proxy through which Angular will send requests: https://angular.io/guide/build#proxying-to-a-backend-server.
See https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS for more information about this subject.