0
votes

I have set up hosting on Firebase and configured my Node.js express app following the documentation provided by Google. Including the proper folder structure and command line instructions to init firebase and firebase-functions.

Folder Structure:

Project
-- functions
   -- node_modules
   -- index.js
   -- package.json
-- public
   -- index.html
   -- 404.html
.firebaserc
firebase.json

I have added the express app to the firebase functions http request via the code below:

// Set up endpoint
app.get('/api', (req, res) => {
    res.json({
        message: 'Welcome to the Line Prophet API.  Good luck.'
    })
});

/**
 * Firebase cloud functions 
 */
exports.app = functions.https.onRequest(app);

My Firebase.json file is set up to direct all request to the app destination:

{
  "hosting": {
    "public": "public",
    "rewrites": [
      {
        "source": "**",
        "destination": "app"
      }
    ]
  }
}

Once I run firebase deploy from my parent directory everything goes through fine and it says the app is deployed: enter image description here

However after that I navigate to https://line-prophet.web.app/api and I recieve a 404 page not found error.
enter image description here

I have tried to run this locally with firebase serve and I have the same issue. This was working briefly which is why I feel as though everything is set up correctly, however after deploying again it has broken for good. Any tips are appreciated. Thanks!

The latest deployments say there are only 4 files deployed to Firebase which seems very low. I checked the Functions tab and I do see that "app" is in there and the source code is correct.

1
FYI: You can't use app.listen in Cloud Functions. The infrastructure manages all the incoming sockets and delivers requests to your functions (when configured correctly).Doug Stevenson
@DougStevenson I figured that might be relevant. I have commented that line out, re-deployed. Facing same issue, thanks for the tip.Waggoner_Keith
did you try replacing "destination": "app" with "function": "index", why use app? its not a file or route, see docsLawrence Cherone
That was basically it thank you so much. I got "destination" from the docs, and I was using app because that is the name of the export associated with http requests. I changed it to "function": "app" (instead of index) and that worked. @LawrenceCheroneWaggoner_Keith
thats great glad it helped (it was a shot in the dark).. too many options for the trees ;pLawrence Cherone

1 Answers

1
votes

Thanks to @LawrenceCherone changing the firebase.json file to:

"rewrites": [
      {
        "source": "**",
        "function": "app"
      }
    ]

solved my issue. I was using destination instead of function because of the online docs, but it makes more sense to direct all requests to the function you have set up to handle http request.