如何托管Firebase功能和单页应用程序?

时间:2018-07-19 23:31:46

标签: javascript reactjs firebase google-cloud-functions


我正在使用React.js开发单页应用程序。我和我的团队将尽我们最大的能力来利用Firebase Hosting,并正在尝试部署我们的应用程序。我的问题是我试图同时部署静态站点和功能,唯一的问题是我无法使用当前配置托管这两个站点。这是我正在使用的firebase.json,尽管它没有显示/helloWorld上的功能,只显示了静态站点。

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/helloWorld",
        "function": "helloWorld"
      },
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

当我转到helloWorld时,我希望它路由到/helloWorld函数,而当我转到{{1}以外的任何路由时,我希望它路由到静态index.html文件}。这是一个例子,如果我是Express.js

/helloWorld

1 个答案:

答案 0 :(得分:3)

如果我理解得很好,您只想在用户尝试访问index.html时将其重定向到www.yourwebiste.com/**(其中**可以是helloWrold之外的任何其他字符)并且仅在他们尝试访问www.yourwebsite.com/helloWorld

时显示 helloWorld

对吗?

如果是,那么我将按照以下方法进行操作:

firebase.json

...
"rewrites": [
  {
   "source": "/helloWorld/**", 
   "function": "helloWorld"
  }
]

无需添加

{
  "source": "**",
  "function": "/index.html"
}

因为这是默认重定向。

然后在您的js文件中

const express = require("express");
const redirection = express();

redirection.route('/helloWorld')
  .get(function (req, res) {
    res.send('this is my redirected page')
  })
相关问题