创建一个catch all redirect&子函数重定向

时间:2018-05-22 20:21:28

标签: firebase firebase-hosting

我的域似乎重定向到index.html,所有子链接都很好。
问题是它不会重定向到api规则"source": "/api/**"
我已经使用整个firebase网址(没有自定义域)手动检查了API并且它可以正常工作。

如何让domain.com/somelink和domain.com/api/somelink同时工作?

这是配置。

firebase.json

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

1 个答案:

答案 0 :(得分:1)

如此处所述https://stackoverflow.com/questions/44959652/firebase-hosting-with-dynamic-cloud-functions-rewrites/45224176#45224176

创建一个托管所有其他顶级功能的主应用程序。

const funtions = require('firebase-functions');
const express = require('express');
const app = express();

app.get('/users/:userId/:userData/json', (req, res) => {
    // Do App stuff here
}
// A couple more app.get in the same format

// Create "main" function to host all other top-level functions
const main = express();
main.use('/api', app);

exports.main = functions.https.onRequest(main);

firebase.json

{
    "source": "/api/**", // "**" ensures we include paths such as "/api/users/:userId"
    "function": "main"
}

使用主钩。

const hooks = express();
main.use('/hooks/, hooks);