Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular-cli server - how to proxy API requests to another server?

With the angular-cli ng serve local dev server, it's serving all the static files from my project directory.

How can I proxy my AJAX calls to a different server?

like image 929
elwyn Avatar asked May 11 '16 20:05

elwyn


People also ask

What is reverse proxy in angular?

A reverse proxy is a server that retrieves resources for clients from one or more upstream servers. It typically places itself behind a firewall in a private network and forwards clients request to these upstream servers.

Why do we use proxy in angular?

Angular CLI uses webpack-dev-server as the development server. The webpack-dev-server makes use of the powerful http-proxy-middleware package which allows us to send API requests on the same domain when we have a separate API back end development server.

How does a proxy work?

A proxy server is a system or router that provides a gateway between users and the internet. Therefore, it helps prevent cyber attackers from entering a private network. It is a server, referred to as an “intermediary” because it goes between end-users and the web pages they visit online.


1 Answers

UPDATE 2017

Better documentation is now available and you can use both JSON and JavaScript based configurations: angular-cli documentation proxy

sample https proxy configuration

{   "/angular": {      "target":  {        "host": "github.com",        "protocol": "https:",        "port": 443      },      "secure": false,      "changeOrigin": true,      "logLevel": "info"   } } 

To my knowledge with Angular 2.0 release setting up proxies using .ember-cli file is not recommended. official way is like below

  1. edit "start" of your package.json to look below

    "start": "ng serve --proxy-config proxy.conf.json",

  2. create a new file called proxy.conf.json in the root of the project and inside of that define your proxies like below

    {   "/api": {     "target": "http://api.yourdomai.com",     "secure": false   } } 
  3. Important thing is that you use npm start instead of ng serve

Read more from here : Proxy Setup Angular 2 cli

like image 69
imal hasaranga perera Avatar answered Oct 19 '22 23:10

imal hasaranga perera