首页 > 解决方案 > Firebase 重写无法在托管站点上运行

问题描述

我一直在尝试让 Firebase 重写在我的 Firebase 托管网站上工作。无论我尝试什么,它都不会起作用。现在,我将所有重写都路由到了一个名为 app 的路由函数。如下所示>>

firebase.json

     {
            "public": "public/dist-main",
            "target": "public-store-easy",
            "ignore": [
                "firebase.json",
                "**/.*",
                "**/node_modules/**"
            ],
            "rewrites": [
                {
                    "source": "/**",
                    "function": "app"
                }
            ]
        },

这是我的路由功能>>

const functions = require('firebase-functions');
const express = require('express');
const path = require('path');
const html = require('html')
const app = express();
app.use(express.static('/../public/'));

app.get('/', (req, res) => {
    res.sendFile('/dist-main/index.html', { root: '../public' })
})
app.get('/login', (req, res) => {
    res.sendFile('/dist-main/login.html', { root: '../public' })
})
app.get('/HOME', (req, res) => {
    res.sendFile('/dist-main/home.html', { root: '../public' })
})
app.get('/feedback', (req, res) => {
    res.sendFile('/dist-main/feedback-form.html', { root: '../public' })
})
exports.app = functions.https.onRequest(app);

我希望它可以正常工作,因为它在我的 localhost 服务器和 firebase 服务器上都可以正常工作。

先感谢您!

标签: node.jsfirebaseexpressgoogle-cloud-functionsfirebase-hosting

解决方案


部署后,Cloud Functions 只能访问目录中的functions文件。您正在尝试发送../public未部署功能的文件。

如果你想让它工作,你需要在目录中移动public目录functions并适当地更新{"hosting": {"public": ""}}密钥。


推荐阅读