1

There is a solution to this error without the use of Firebase here when using app.listen(8080) however this does not work while serving in cloud functions with exports.app = functions.https.onRequest(app)

Here is a simple reproduction code

const app = express();
app.get('**', (req, res) => res.send('working'));
app.use((err, req, res, next) => res.redirect('/404'));
exports.app = functions.https.onRequest(app) // doesnt work
// app.listen(5000) // works

How do you go about catching this error in firebase functions? I would like the redirect to work.

firebase test: firebase serve --only functions

express serve: node index.js

URL to test: http://localhost:5000/%CO

Note that the additional %CO is the one that cannot be decoded by express. This error is caught while serving with the express method but not with the firebase functions method.

As this seems like a bug, I have also created an issue here on github incase I find no workaround on it.

Lucem
  • 2,912
  • 3
  • 20
  • 33
  • I inspected the firebase logs and found why the error is happening but I dont know how to go about it yet. `AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\layer.js:172:12)` is catching `decodeURIComponent(val)` error before express receives the request. How can i override this? – Lucem Nov 06 '21 at 06:34

2 Answers2

0

Try changing the name of app in exports.

exports.newApp = functions.https.onRequest(app) 
Huthaifa Muayyad
  • 11,321
  • 3
  • 17
  • 49
0

According to the documentation, the correct way of using an Express app with Firebase Functions is to pass the application to a Function like:

// Expose Express API as a single Cloud Function:
exports.app = functions.https.onRequest(app);

Listening to a port for requests does not apply when running through Firebase Functions. As for how to catch errors when passing your app to a function, it’s done in the same way as the question you referenced as far as I have reviewed. You can catch errors by using Express middleware while using Cloud Functions.

Moreover, implementing redirects with Firebase Functions is explained in this related question, which makes use of the documentation to configure how to redirect by modifying the firebase.json file.

ErnestoC
  • 2,660
  • 1
  • 6
  • 19
  • Thank you for your answer, I have editted the question to make it more clear since your answer did not touch on the issue that I am facing. The serving works but express middleware doesnt work while using firebase – Lucem Nov 06 '21 at 05:55
  • I read your comment under the post, and based on this related [thread](https://stackoverflow.com/questions/63347239/), it seems you are in a similar scenario. Instead of catching the error with an error handler, you should create app middleware which catches the error and redirects users to your desired page. I would recommend following the guide about [routing middleware](https://expressjs.com/en/guide/routing.html). – ErnestoC Nov 08 '21 at 17:23