Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy express app on firebase hosting

I have built an express app and the folder structure is below.

as follows

Then i created firebase init hosting on a dummy folder and copied the firebase.json and .firebase files

I created index.js file

    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/**"
    ]
  }
}

Also copied the index.html generated by firebase to public folder

enter image description here

On deployment i am getting index.html

enter image description here

If i delete index.html and run as localhost i am getting below output

enter image description here

How can i get the express app executed (as shown in localhost) instead of index.html on firebase deploy.

Edit 1

I am following link.

When i run firebase serve, i am getting this error

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)

When i tried to run 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.

The reason for this is missing public folder, and i created manually (which was expected to be created with firebase init functions).

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

Now my deployment is successful. But i am getting 404

Answer In the index.js file (following the above link), i did not change

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

to

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

Thanks to all for the support

like image 238
Alaksandar Jesus Gene Avatar asked May 22 '19 16:05

Alaksandar Jesus Gene


People also ask

Can I host Nodejs in firebase?

Firebase Hosting supports a REST API for advanced developers to build custom workflows, like deploying through a JavaScript app. We also have a Node. js module which you can import into your Node. js apps to build advanced functionality.


1 Answers

Firebase Hosting prefers to serve static content over rewrites that get sent to Cloud Functions. In other words, if a request could be served by any static content, that content will always take precedence over a rewrite to Cloud Functions.

If you want your web site root page to be served by Cloud Functions, this means you should not have an index.html in your public folder, since Firebase Hosting is finding that first.

like image 147
Doug Stevenson Avatar answered Oct 02 '22 23:10

Doug Stevenson