使用 Firebase 重写将 /api/** 路由到函数,将其他所有内容路由到单页应用程序

ler*_*ros 3 firebase firebase-hosting

我已经看到其他堆栈溢出答案,使用 Firebase 重写将 /api/** 路由到其 Express 应用程序功能。

我正在遵循这些说明,但也尝试托管单页应用程序。

将两者结合起来似乎不起作用,因为 API 路由仍然映射到我的 index.html 文件。

这些是我的重写

"rewrites": [
  {
    "source": "/api/**",
    "function": "api"
  },
  {
    "source": "**",
    "destination": "/index.html"
  }
]
Run Code Online (Sandbox Code Playgroud)

这可能吗?

Ger*_*ace 5

为了回答您的问题,我设法通过从单页应用程序规则中排除 /api/ 来做到这一点。

"rewrites": [
  {
    "source": "/api/**",
    "function": "api"
  },
  {
    "source": "!/api/**",
    "destination": "/index.html"
  }
]
Run Code Online (Sandbox Code Playgroud)

在这里我们说:

  • 以“/api/”开头的所有内容都会转到名为“api”的函数
  • 其他所有内容都将转到您的单页应用程序(index.html)