Firebase 函数不使用 express.js 和 EJS 视图引擎提供静态文件

Geo*_*rji 6 ejs node.js express google-cloud-functions

过去一个月我一直在研究这个问题,但没有找到任何有用的解决方案。我目前正在尝试使用 firebase 函数以及 express.js 和 EJS 视图引擎来托管网站。

我将讨论将静态 CSS 文件提供给 EJS 文件的问题。我已经看遍了,但似乎找不到任何有帮助的正确答案。

我有以下文件结构(在函数文件夹内):

  • 民众
  • 意见
  • 索引.js

这是我的 index.js:

const functions = require('firebase-functions');
const express = require("express");
const ejs = require("ejs");
var path = require('path');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });

var app = express();

app.set('view engine', 'ejs');

// app.use('/static', express.static('public'));
app.use(express.static(__dirname + "/public"));
app.get("/", (request, response) => {
    response.render("test");
})

// app.listen(5000, () => {
    // console.log("Active app on port 5000");
// });
exports.webApp = functions.https.onRequest(app);
Run Code Online (Sandbox Code Playgroud)

它正在呈现的页面(test.ejs):

<html>

<head>

    <link rel="stylesheet" href="style.css" type="text/css">
</head>


<body></body>
</html>
Run Code Online (Sandbox Code Playgroud)

最后,style.css 文件:

html {
    background-color: black;
}
Run Code Online (Sandbox Code Playgroud)

(style.css 与 test.ejs 一样仅用于测试,因为我对所有这些都遇到了大问题)

我在网上尝试了大多数解决方案,但是所有解决方案都只能在本地运行 express.js 应用程序时起作用,而不是通过 firebase serve 或 deploy 命令。我完全迷失在这方面,任何帮助表示赞赏。

zem*_*nkh 1

问题是由 Google Cloud 计划引起的。

一开始,所有 Firebase 用户都选择完全免费的 Spark 计划。最近,Google Cloud可能屏蔽了我们一年前免费使用的一些功能。

因此,您需要转到Google Cloud 控制台,然后更改要部署的项目的计划。然后,如果您没有注册支付卡,则应该为firebase 托管firebase 函数功能进行注册。 在此输入图像描述

几分钟后,它会加载所有必需的文件,例如加载部署的网站时缺少的样式 CSS、JS 文件。

然后享受您部署的网站。;)