angular-cli服务器-如何将API请求代理到另一台服务器? [英] angular-cli server - how to proxy API requests to another server?

查看:100
本文介绍了angular-cli服务器-如何将API请求代理到另一台服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用angular-cli ng serve本地开发服务器,它可以为我的项目目录中的所有静态文件提供服务.

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

如何将我的AJAX呼叫代理到其他服务器?

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

推荐答案

UPDATE 2017

更好的文档现在可用,您可以同时使用 基于JSON和JavaScript的配置: angular-cli文档代理

UPDATE 2017

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

https代理配置示例

sample https proxy configuration

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


据我所知,Angular 2.0版本不建议使用.ember-cli文件设置代理.官方方式如下


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

  1. 编辑package.json"start"使其显示在下方

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

在项目的根目录中创建一个名为proxy.conf.json的新文件,并在其中定义您的代理,如下所示

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
  }
}

  • 重要的是,您使用npm start而不是ng serve

  • Important thing is that you use npm start instead of ng serve

    从此处了解更多信息:代理设置Angular 2 cli

    Read more from here : Proxy Setup Angular 2 cli

    这篇关于angular-cli服务器-如何将API请求代理到另一台服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆