如何在 Firebase 主机上部署 express 应用程序

Posted

技术标签:

【中文标题】如何在 Firebase 主机上部署 express 应用程序【英文标题】:How to deploy express app on firebase hosting 【发布时间】:2019-10-09 05:14:18 【问题描述】:

我已经构建了一个快递应用,文件夹结构如下。

然后我在一个虚拟文件夹上创建了 firebase init 托管并复制了 firebase.json 和 .firebase 文件

我创建了 index.js 文件

    const functions = require('firebase-functions')
    const app = require('./app');
    exports.widgets = functions.https.onRequest(app);

firebase.json


  "hosting": 
    "public": "public",
    "rewrite":[
      "source": "**",
      "function": "widgets"
    ],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  

还将firebase生成的index.html复制到public文件夹中

在部署时我得到 index.html

如果我删除 index.html 并作为 localhost 运行,我会低于输出

如何在 firebase deploy 上执行 express 应用程序(如 localhost 所示)而不是 index.html。

编辑 1

我关注link。

当我运行 firebase serve 时,我收到此错误

AssertionError [ERR_ASSERTION]: missing path at Module.require (module.js:583:3) at require (internal/module.js:11:18) at InitializeFirebaseAdminStubs (C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:231:18) at C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:451:9 at Generator.next () at C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:7:71 at new Promise () at __awaiter (C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:3:12) at main (C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:421:12) at Object. (C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:511:5)

当我尝试运行 firebase deploy

E:\ogst-server-firebase\functions>firebase deploy

=== Deploying to 'ogst-server-95fcc'...

i  deploying functions, hosting
i  functions: ensuring necessary APIs are enabled...
+  functions: all necessary APIs are enabled

Error: An unexpected error has occurred.

原因是缺少公用文件夹,而我是手动创建的(预计将使用 firebase init 函数创建)。

E:\ogst-server-firebase\functions>firebase deploy

=== Deploying to 'ogst-server-95fcc'...

i  deploying functions, hosting
i  functions: ensuring necessary APIs are enabled...
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  hosting[ogst-server-95fcc]: beginning deploy...
i  hosting[ogst-server-95fcc]: found 0 files in public
+  hosting[ogst-server-95fcc]: file upload complete
i  hosting[ogst-server-95fcc]: finalizing version...
+  hosting[ogst-server-95fcc]: version finalized
i  hosting[ogst-server-95fcc]: releasing new version...
+  hosting[ogst-server-95fcc]: release complete

+  Deploy complete!

Please note that it can take up to 30 seconds for your updated functions to propagate.
Project Console: https://console.firebase.google.com/project/ogst-server-95fcc/overview
Hosting URL: https://ogst-server-95fcc.firebaseapp.com

现在我的部署成功了。但我得到 404

回答 在 index.js 文件中(按照上面的链接),我没有改变

module.exports = functions.https.onRequest(app); //wrong

to

exports.app = functions.https.onRequest(app); //correct

感谢大家的支持

【问题讨论】:

【参考方案1】:

Firebase 托管更喜欢提供静态内容,而不是发送到 Cloud Functions 的重写内容。换句话说,如果请求可以由任何静态内容提供服务,则该内容将始终优先于对 Cloud Functions 的重写。

如果您希望 Cloud Functions 为您的网站根页面提供服务,这意味着您的公共文件夹中不应有 index.html,因为 Firebase 托管会首先找到它。

【讨论】:

那么这可能就是您的快递应用所提供的服务。 no..如果我删除 index.html 并运行 npm start,我正在执行我的 express 应用程序。 npm start 不属于涉及 Firebase 托管的工作流。您需要使用 Firebase CLI 开始模拟 Firebase 托管及其重写。 firebase serve 开始。请查看托管文档以更好地理解这一点。 你能看看我的编辑1吗 谢谢我修好了

以上是关于如何在 Firebase 主机上部署 express 应用程序的主要内容,如果未能解决你的问题,请参考以下文章