Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase functions: koa.js server how to deploy

I already have an app written in MERN stack with koa server prepared build version. My main node file to run by node server.js command to start the whole app looks like this.

In every tutorial, I see that I need to add functions.https.request etc. in the beginning of coding (or at least to suppose doing it). How could I host my app on firebase the same as I could on heroku - with whole server side?

like image 349
Kamil Lewandowski Avatar asked Dec 05 '25 14:12

Kamil Lewandowski


2 Answers

It is possible to host Koa app using firebase functions, I figure it out after some Googling and analyzing.

This is a piece of code from my project, it is now hosted with firebase functions:

const Koa = require('koa');
const app = new Koa();

// ... routes code here ...

// This is just for running Koa and testing on the local machine
const server = app.listen(config.port, () => {
  console.log(`HITMers-server is running on port ${config.port}`);
});
module.exports = server;

// This export is for Firebase functions
exports.api = functions.https.onRequest(app.callback());

You can see the docs and tutorial video for more information.

By the way, here is another example to deploy Koa to now.sh version 2.

like image 191
upupming Avatar answered Dec 08 '25 16:12

upupming


You can actually skip the listen call entirely, and use app.callback(). This seems to make more sense than listening on a random port that never actually gets hit.

const functions = require('firebase-functions');
const app = new Koa();
... // set up your koa app however you normally would
app.use(router.routes());
module.exports.api = functions.https.onRequest(app.callback());
like image 38
theozero Avatar answered Dec 08 '25 17:12

theozero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!