0

Now I created a file by node server like

const functions = require("firebase-functions")
const express = require("express")

/* Express */
const app = express()
app.get("/test", (request, response) => {
  response.send("Hello from Express on Firebase!")
})

const api1 = functions.https.onRequest(app)


module.exports = {
  api1
}

firebase.json

    {
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ]
  }
}

and I have deployed it to firebase hosting and try tried access it on firebase hosting the index.html show up to me but when I require the /test it's return as 404.html page ! what's I'm missing ?

after updated firebase.json and added rewrites to

{
  "hosting": 
  {
    "public": "public",
    "rewrites": [
      {
          "source": "/test",
          "function": "app"
      }
    ]
  }
}

the message is different now enter image description here


The answer


I must to structured my project files to -project (folder) ---functions (folder)(contains on all nodejs files) ---public (filder)

Mahmoud Niypoo
  • 1,598
  • 5
  • 24
  • 41

1 Answers1

1

You need to include a rewrites section to your firebase.json file. That tells the Firebase servers how to route any requests that come in... and right now, you aren't telling it anything.

"hosting": {
    "rewrites": [
        {
            "source": "**",
            "function": "api1"
        }
    ]
}

This answer isn't actually an answer to your question, but it demonstrates the proper way to set up rewrites with cloud function express apps.

JeremyW
  • 5,157
  • 6
  • 29
  • 30
  • and how about if I have several routes like const users= require('./routes/users'); const books= require('./routes/books'); app.use('/users', users); app.use('/books', books); – Mahmoud Niypoo Mar 21 '19 at 15:44
  • after I added the rewrites the message became different "404. That’s an error. The requested URL was not found on this server. That’s all we know." – Mahmoud Niypoo Mar 21 '19 at 15:55
  • @MahmoudNiypoo You mean that after you've added the rewrites, and you visit `/test?` or `/`? then you get the 404? Any other route other than `/test` will 404 because it isn't configured in your Express instance. If you had other routes then you'd just have to include them in your express setup, just like any other node.js project. The rewrite sends ALL requests (*the double astrisk*) to your express app at the moment. – JeremyW Mar 21 '19 at 15:58
  • in this moment I only have just one route`/test` that happened when call root `/` the index.html appear to men but when call `/test' the message that I added in top appear to me **404. That’s an error. The requested URL was not found on this server. That’s all we know** – Mahmoud Niypoo Mar 21 '19 at 16:03
  • @MahmoudNiypoo Hmmm, well that doesn't make any sense to me - with your rewrites section, `/test` SHOULD be showing your Hello message. – JeremyW Mar 21 '19 at 16:30
  • @MahmoudNiypoo I see your update - check your "function" value in your rewrite rule. You have `app` whereas you need `api1` – JeremyW Mar 21 '19 at 16:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190454/discussion-between-jeremyw-and-mahmoud-niypoo). – JeremyW Mar 21 '19 at 16:32
  • Finally i found what was wrong , I must sort my files as project/functions/nodejs files nad project/public/assets files – Mahmoud Niypoo Mar 21 '19 at 19:31
  • @MahmoudNiypoo Thanks for coming back and letting me know - I've been scratching my head the whole afternoon about what it was. – JeremyW Mar 21 '19 at 21:14