javascript - 如何托管 Firebase 函数和单页应用程序?

标签 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 函数,并将其路由到静态 index.html 文件当我转到 /helloWorld 以外的任何路线时。这是一个示例如果我是 Express.js

const express = require('express');
const app = express();

app.get('/helloWorld', (req, res) => {
  res.send('this is a cloud function');
});

app.get('*', (req, res) => {
  res.send('this is the static site');
});


app.listen(3000);

最佳答案

如果我理解得很好,您只需在用户尝试访问 www.yourwebiste.com/** 时将他们重定向到 index.html (其中 ** 可以是 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')
  })

关于javascript - 如何托管 Firebase 函数和单页应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51433084/

相关文章:

javascript - 如何使用 google maps api v3 旋转 map 方向

javascript - JQuery 和 Colorbox 加载但 "colorbox is not a function"

swift - 在闭包外部访问 Firebase 变量

swift - 如何在 swift 中进行顺序 firebase 查询?

javascript - React 组件检查变量是否为空

java - 安卓火力地堡 : Second Connected Database not allowing auth in rules

javascript - 有人可以解密这个javascript

javascript - 读取 txt 文件的行并组织在 JSON 文件中

javascript - React Native 端口 8081 问题 : 403 Forbidden

android - React Native Android TV 控件