[英]How can you host firebase functions and a single-page application?
我正在使用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/**
/ **
(其中**
可以是helloWrold
其他東西)時將其重定向到index.html
,並且僅在他們嘗試訪問helloWorld頁面時到達www.yourwebsite.com/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')
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.