如何在Firebase托管上部署Express应用

时间:2019-05-22 16:41:12

标签: firebase express google-cloud-functions firebase-hosting

我已经构建了一个Express应用,文件夹结构在下面。

as follows

然后我在虚拟文件夹上创建了Firebase初始化托管,并复制了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复制到公用文件夹

enter image description here

在部署时,我会得到index.html

enter image description here

如果我删除index.html并以localhost身份运行,我将得到以下输出结果

enter image description here

如何在Firebase部署中使Express应用程序执行(如localhost中所示)而不是index.html。

编辑1

我正在关注link

运行 firebase服务时,出现此错误

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部署

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 个答案:

答案 0 :(得分:0)

Firebase Hosting倾向于提供静态内容,而不是发送给Cloud Functions的重写。换句话说,如果任何静态内容都可以满足请求,则该内容将始终优先于重写Cloud Functions。

如果您希望Web站点根页面由Cloud Functions提供服务,这意味着您的公用文件夹中不应包含index.html,因为Firebase Hosting会首先找到它。