Angular 2 with REST-Backend

I’ve recently started a project based on Angular 2 for the front-end, and with a full-REST/Webservices backend.

While doing some devs for the front, as many of us, I used the famous “ng serve” to get a fresh compiled-over-the-time GUI. However, to serve the backend, I faced a very classic “CORS” issue (you know, this cross-over reference blockout form your browser, even with localhost as your server).

So, in that case, 2 ways:
– Allow CORS (whatever the way)
– Use the “ng serve” as a proxy for your backend.

The 2nd option is very simple to setup:
1) Create a proxy.conf.json file at the root of your angular2 app with the following content

{
  "/api": {  /** the root-path for your backend **/
     "target": "http://localhost:8080",   /** your REST-backend access **/
     "secure": false
  }
}

2) Serve through “ng serve” with the following command-line

ng serve --proxy proxy.conf.json

And that’s all….

Leave a comment