NodeJS 重新路由Firebase托管以实现云功能

k4aesqcs  于 2022-12-03  发布在  Node.js
关注(0)|答案(1)|浏览(143)

我似乎无法配置路由来连接firebase主机到我的云函数express应用程序。我试图设置为shown here,但行为似乎不同。我似乎不知道我做错了什么。请发送帮助,我快疯了。

索引.js

const functions = require("firebase-functions");

const app = require('./app');
exports.api = functions.https.onRequest(app);

应用程序.js

const app = express();
// ...
app.use(cors());
app.use('/params', paramsRoutes);
module.exports = app;

防火墙.json

{
  "hosting": {
    // ...
    "rewrites": [
      {
        "source": "/api/**",
        "function": "api"
      },
     // ...
    ]
  }
}
kqlmhetl

kqlmhetl1#

There is appropriate documentation that lists your requirement of hosting the Cloud Functions in Firebase Hosting using a custom domain.

  • Make sure that you have the latest version of the Firebase CLI and that you've initialized Firebase Hosting.
  • Initialize Cloud Functions by running the following command from the root of your project directory: firebase init functions

Also as per documentationwe can use rewrites to serve requests for a specific firebase hosting URL to a function. To do that you need to use "/api". You don't have to add the entire URL because you are redirecting from firebase hosting. So, when you add "/api" you are indicating to redirect requests going to "PROJECT_ID.web.app/api and PROJECT_ID.firebaseapp.com/api" to the function you specified.
If you want redirect every single URL to your host to an express app in Cloud Functions, you will need to do the following:

  • Make sure there is no index.html in your public hosting folder (otherwise it will always be served with the path /).
  • Your Express routes need to exactly match the incoming URL, not just the wildcard part.

You can read more in the docs or follow this medium post for more details.
Check these links for similar implementation:

相关问题