user3062513
user3062513

Reputation: 410

Implementing CORS in Angular+ Node

I have a impolemented frontend in Node+Angular js and backend is running in Jetty exposing REST service.I want to send REST rquest from FE(Angular) to backend.when I am passing the complete URL to $http.get it works fine.However I want to mak it a configurable value so that any change in the URL/Port will not require me to change elsewhere apart from the configuration.Currently I am passing it as $http.post("http://localhost:8080/test/login/doStuff",dataObj) However I want to pass it as $http.post("/test/login/doStuff",dataObj) and automatically it should append http://localhost:8080.Can someone help me on this?

Thanks

Upvotes: 0

Views: 73

Answers (2)

bajran
bajran

Reputation: 1503

You can store the hostname in another variable and concat that with url

let host = "http://localhost:8080"

$http.post(host + "/test/login/doStuff", dataObj)

Upvotes: 0

Aviro
Aviro

Reputation: 2155

CORS is used to enable the communication trust between two different hosts. For example if the front end and back-end hosted in two different servers (not in the same domain) requires CORS to communicate in between.
However, in your case, I do not see a valid case except they deployed apart as explained.
A web app always can communicate using relative paths as long as they are hosted in the same server.
If the app hosted inside /myApp folder, it can access other resources using /myApp/other-web-folders.
If you want to communicate outside domain, you always have to do it with the full url. Hope this clarifies your doubts.

Upvotes: 1

Related Questions