首页 > 解决方案 > 如何使用firebase函数从父文件夹中读取文件

问题描述

app.get('/sign-in', (req, res) =>{
    res.writeHead(200);
    res.end(fs.readFileSync('../html/Login.html'));
})

app.get('/sign-up', (req, res) =>{
    console.log('connecting sign-up BABY');
    res.writeHead(200);
    res.end(fs.readFileSync('../html/Register.html'));
})

如您所见,我正在使用 fs.readFilesync 来调用 html 文件。这很奇怪,因为它通过使用在 localhost 上firebase serve工作,但在部署后它在 firebase 服务器上不起作用。错误信息更令人困惑。它只说Error: could not handle the request并且控制台显示

Function execution started
Function execution took 1559 ms, finished with status: 'connection error'

标签: javascriptnode.jsfirebasegoogle-cloud-functions

解决方案


当您部署 Cloud Functions 时,Firebase 会部署文件夹中的functions文件。您的代码似乎正在尝试从不同的文件夹中读取文件。由于该文件夹尚未部署到 Cloud Functions,因此它失败了。

您从 Cloud Functions 代码中读取的任何内容都应位于该functions文件夹中/下。您可能希望在部署之前将文件夹移动/复制到文件夹html中。files


推荐阅读