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).

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:

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.

Configure a proxy for your API calls with Angular CLI的更多相关文章

  1. FaceBook登陆API -- Login with API calls

    Login with API calls Related Topics Understanding sessions FBSession Error handling FBError FBLoginC ...

  2. Docker configure http proxy

    from: http://stackoverflow.com/questions/23111631/cannot-download-docker-images-behind-a-proxy That' ...

  3. 【Android Studio错误】 If you are behind an HTTP proxy, please configure the proxy settings either in IDE or Gradle.

    解决办法:以管理员身份运行cmd窗口,输入命令“netsh winsock reset” netsh winsock reset命令,作用是重置 Winsock 目录.如果一台机器上的Winsock协 ...

  4. Throttling ASP.NET Web API calls

    http://blog.maartenballiauw.be/post/2013/05/28/Throttling-ASPNET-Web-API-calls.aspx https://github.c ...

  5. [Unit Testing] Configure the Angular CLI to use the Karma Mocha test reporter

    Every Angular CLI generated project comes already with Karmapreinstalled as well a couple of executa ...

  6. [Angular] Configure an Angular App at Compile Time with the Angular CLI

    Compile time configuration options allow you to provide different kind of settings based on the envi ...

  7. Using Amazon API Gateway with microservices deployed on Amazon ECS

    One convenient way to run microservices is to deploy them as Docker containers. Docker containers ar ...

  8. puppeteer(五)chrome启动参数列表API

    List of Chromium Command Line Switches https://peter.sh/experiments/chromium-command-line-switches/ ...

  9. API 'variant.getJavaCompiler()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()'

    WARNING: API 'variant.getJavaCompiler()' is obsolete and has been replaced with 'variant.getJavaComp ...

随机推荐

  1. 关于spring配置文件中编辑时没有提示信息的问题

    spring配置文件头部信息主要是提供一个xml的编写规范作用. 新创建的配置文件引入头部信息后,编辑时没有提示信息,重启elipse即可解决.

  2. Python 返回多个值+Lambda的使用

    def MaxMin(a,b): if(a>b): return a,b else: return b,a max,min=MaxMin(8,95) print "最大值为:" ...

  3. 14_传智播客iOS视频教程_OC的数据类型

    对比一下OC和C差别,首先第一个是数据类型. C语言的数据类型分哪几类?C语言有哪些数据类型? 基本数据类型当然还包括int的一些修饰符.像short.long.long long.unsigned. ...

  4. Linux中的LVM

    逻辑卷管理器,通过将另外一个硬盘上的分区加到已有文件系统,来动态地向已有文件系统添加空间的方法. 逻辑卷管理的核心是处理安装在系统上的硬盘分区.在逻辑卷管理的世界里,硬盘称作物理卷(Physical ...

  5. bzoj 1778: [Usaco2010 Hol]Dotp 驱逐猪猡【dp+高斯消元】

    算是比较经典的高斯消元应用了 设f[i]为i点答案,那么dp转移为f[u]=Σf[v]*(1-p/q)/d[v],意思是在u点爆炸可以从与u相连的v点转移过来 然后因为所有f都是未知数,高斯消元即可( ...

  6. [C陷阱和缺陷] 第5章 库函数

      有关库函数的使用,我们能给出的最好建议是尽量使用系统头文件,当然也可以自己造轮子,随个人喜好.本章将探讨某些常用的库函数,以及编程者在使用它们的过程中可能出错之处.   5.1 返回整数的getc ...

  7. javascript面试题集

    1.如何把一句英文每个单词第一个字母大写? var str = "what fuck is 1235 going on ?"; var newArr = str.split(&qu ...

  8. 300 Longest Increasing Subsequence 最长上升子序列

    给出一个无序的整形数组,找到最长上升子序列的长度.例如,给出 [10, 9, 2, 5, 3, 7, 101, 18],最长的上升子序列是 [2, 3, 7, 101],因此它的长度是4.因为可能会有 ...

  9. 如何手工搭建本地Yum仓库

    如何手工搭建本地Yum仓库(重点推荐)  https://www.linuxidc.com/Linux/2016-09/135480.htm CentOS7.2 创建本地YUM源和局域网YUM源: h ...

  10. P1165 日志分析

    题目描述 M 海运公司最近要对旗下仓库的货物进出情况进行统计.目前他们所拥有的唯一记录就是一个记录集装箱进出情况的日志.该日志记录了两类操作:第一类操作为集装箱入库操作,以及该次入库的集装箱重量:第二 ...