0

Im trying to run an Express app into firebase, i'm following the steps of this official video:

Node.js apps on Firebase hosting crash course

My Express app is actually running on this URL

http://localhost:5001/my-app/us-central1/app/test

But on the video, the demostration is running on this url

http://localhost:5000/test

So, i'm really confused about this, i'been made everything as the tutorial shows

this is my code on functions/index.js

const functions = require('firebase-functions');

const express = require('express')
const app = express()

app.get('/test', (req, res) => {
    res.send(`Well if i'm reading this the app is working`)
})


exports.app = functions.https.onRequest(app)

And this is firebase.json

{
  "hosting": {
    "public": "public",
    "rewrite":[{
      "source": "/test",
      "function": "app"
    }],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

What i'm doing wrong? if i enter to localhost:5000 i just get the public static index.html, i just want to control the app by myself.

I hope you can give me a little help to get documented, thanks!

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • The `firebase.json` maps `/test` to your app, so `localhost:5000/test` should serve the express app. – Frank van Puffelen May 22 '18 at 02:24
  • That's the logical behavior but isn't working on that way, instead is rendering the public/index.html file – Sebastian Altamirano May 22 '18 at 02:37
  • How are you running the functions locally? – Frank van Puffelen May 22 '18 at 02:37
  • This is printed by the console: functions: Preparing to emulate functions. Warning: You're using Node.js v8.11.2 but Google Cloud Functions onl 6.11.1. i hosting: Serving hosting files from: public + hosting: Local server: http://localhost:5000 + functions: app: http://localhost:5001/r-commerce/us-central1/app – Sebastian Altamirano May 22 '18 at 02:38
  • So it looks like your app locally is being server from `localhost:5001/r-commerce/us-central1/app`. What happens if you open that URL. – Frank van Puffelen May 22 '18 at 03:22
  • If i open that url the app runs right, i deleted index.html from public folder and now console prints this `[hosting] Rewriting /test to local function app` And now the app is running on `localhost:5000/test`. half of the answer, still don't work on deploy – Sebastian Altamirano May 22 '18 at 03:37
  • Delete index.html but conserve 404.html solves the problem, Thanks For your help Frank! – Sebastian Altamirano May 22 '18 at 03:55

1 Answers1

2

The issue here is that Firebase Hosting will serve static content that matches any paths before using any rewrites. If you want to control a path with Cloud Functions, you will have to make sure that there is no static content that matches the path.

For single page apps in particular, it's critical to remove index.html, as that will always be served as static content.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441