欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Configure a proxy for your API calls with Angular CLI

程序员文章站 2022-03-28 19:06:16
Table of contents Local development setup with Angular Issue: Dev server plus backend API Configuring your Angular CLI dev-server proxy Local developm ......

table of contents

local development setup with angular

nowadays you most probably use the angular cli (or some extension of it) for generating your local development setup. this also includes the ng serve command which is wired up in your package.json that gets generated:

{
    ...
    "scripts": {
        "start": "ng serve"
    }
}

as such, when you execute npm startng serve will be invoked which is a command to the cli to fire up its internal development server. that server is based on the webpack dev server (at least as of now). you can then head over to http://localhost:4200 on your web browser and see your running application which is served straight from the file system. behind the scenes happens a lot more, though (in the standard angular setup). the webpack dev server is configured to automatically do the typescript compilation, compile eventual sass to css and serve assets, all in memory without touching the file system (for perf reasons). also it happens to automatically refresh the browser whenever something on your file system changes.

issue: dev server plus backend api

things get a bit more interesting in a more real world setup where you have your angular cli development server running on http://localhost:4200 and your backend api (in whichever technology you like) running on some other port or even host, let’s say http://localhost:3000 (this could also be http://dev-123.mycompany.com or something else ofc).

Configure a proxy for your API calls with Angular CLI
local angular cli dev server without proxy

when you then want to execute an http call within the app, you’d have to write something like this, indicating the full path of your backend api.

this.http.get('http://locahost:3000/api/users')
    .map(res => res.json());

obviously the base url (i.e. the host) can be configured in a central place somewhere, such as via the environment.ts file (that gets generated by the angular cli). but there’s another issue as well. unless you’re creating some publicly consumable api and you inject the required cors headers, you’ll most probably get some cors exceptions.

configuring your angular cli dev-server proxy

there are different options:

  • add the proper cors headers - this is definitely a must in case when you design a generic api where you don’t even know the consumer.
  • use a proxy - a proxy is a piece of software which is in between your javascript/angular app doing the ajax request and your backend api. this is the choice to go in a classic app.

we will take a closer look at the 2nd approach here. what the dev-server proxy does is to simply take the browser request at the same domain+port where you frontend application runs and then forwards that request to your backend api server. cors is a browser security issue and does not apply when doing “backend to backend” communication as is the case with a proxy in between.

the http call inside our angular application can then be changed to this:

this.http.get('/api/users')
    .map(res => res.json());

 

with the proxy, our initial diagram would change to something like this:

Configure a proxy for your API calls with Angular CLI
local angular cli dev server with active proxy

let’s now see how we can setup our dev-server proxy. the angular cli uses webpackunderneath. as a result also the dev-server we’ve been talking about so far is nothing else than the webpack development server. thus, the same configuration settings apply, which are described very well in the official webpack docs.

to set it up, we need to create a file proxy.conf.json at the root of our angular cli project. the content should look as follows

{
  "/api/*": {
    "target": "http://localhost:3000",
    "secure": false,
    "loglevel": "debug",
    "changeorigin": true
  }
}

 

all requests made to /api/... from within our application will be forwarded to http://localhost:3000/api/....

note the changeorigin property. you will definitely have to set this to true when you’re using some virtual proxies (such as configured with apache2) on your backend.