Firebase托管上的Angular 9项目无法通过https请求 [英] Angular 9 project on firebase hosting failed to pass https requests

查看:102
本文介绍了Firebase托管上的Angular 9项目无法通过https请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在如下的角度项目上使用了代理.

I have use a proxy on my angular project as below.

{
  "/api": {
    "target" : "https://<domain_name>/",
    "secure": false
  }
}

通过执行以下命令,它可以与ssl在本地完美地用于HTTPS POST请求

it is perfectly worked in locally with ssl for HTTPS POST requests by executing the command of,

ng serve --ssl true --proxy-config proxy.conf.json

但是在将其托管到firebase托管之后,它总是在每个POST请求中给出以下错误.

But after I host it to the firebase hosting, it always gives below error on every POST request.

Http failure during parsing for https://<app_name>.firebaseapp.com/api/<route_name>

推荐答案

Angular开发服务器提供的功能是在本地环境中运行Angular应用以重定向某些URL段时所包含的代理配置文件.

The proxy config file you are including while running the Angular app in local environment to redirect certain URL segments is a feature provided by the Angular dev server.

由于您使用的是Firebase,因此Firebase还提供了可以在 firebase.json 文件中配置的类似重定向功能.由于您正在使用Firebase进行部署,因此我假设您已经有了firebase.json文件.如果不是,请访问 Firebase CLI 官方文档以将其安装在您的本地环境中.现在运行 firebase init 命令将生成一个基本的firebase.json文件.

Since you are using firebase, firebase also provides a similar redirecting feature that can be configured in the firebase.json file. Since you are using firebase for deployment, I assume you already have a firebase.json file. If not, visit Firebase CLI official docs to install it in your local environment. Now running firebase init command will generate a basic firebase.json file.

包括重定向选项,如下例所示:

Include the redirect option as shown in the example below:

firebase.json

"hosting": {
  // ... other configurations ...
  "redirects": [ {
    "source": "/api/:path*",
    "destination": "https://<gcp_domain_name>/:path",
    "type": 301
  }
}

让我们详细介绍一下我们所做的事情.source属性将URL段作为Firebase进行重定向的值.:path * 指定在/api 之后的任何URL段都应捕获并存储在变量:path 中.现在,在destination属性中,指定目标域,然后指定/:path 来执行重定向.type属性指定 Http响应代码301 来表示永久重定向(在您的情况下).您可以在redirects数组中添加多个重定向.

Let's breakdown what we just did. The source property takes a URL segment as value for Firebase to perform redirection. :path* specifies that any URL segment after /api should captured and stored in the variable :path. Now in the destination property, specify the destination domain followed by /:path to perform the redirect. The type property specifes the Http Response code 301 to represent the permanent redirect (in your case). You can add more than one redirection in the redirects array.

有关Firebase中重定向配置的更多信息,请参见此页面来自Firebase官方文档.除了重定向之外,您还可以找到可以在Firebase托管中配置的所有选项.

For more information on the redirection configuration in Firebase, refer this page from Firebase official docs. In addition to redirection, you can find all options that can be configured in Firebase hosting.

这篇关于Firebase托管上的Angular 9项目无法通过https请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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