2

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?

Whooper
  • 575
  • 4
  • 20
Kamil Lewandowski
  • 396
  • 1
  • 5
  • 15

5 Answers5

9

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.

upupming
  • 1,437
  • 1
  • 12
  • 15
6

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());
theozero
  • 592
  • 5
  • 8
  • Correct answer! – Tony O'Hagan Apr 06 '19 at 08:08
  • 1
    In may case I get request timeout, I tried `functions.https.onRequest(app.callback())`, `functions.https.onRequest((req, res) => app.callback(req,res))` And when I do `functions.https.onRequest(app.callback)` I get `Cannot read property 'middleware' of undefined` – Reza Jun 02 '19 at 06:37
0

You can run an express application using firebase hosting to serve dynamic content via firebase functions. You cannot, however, use Koa.js currently. The functions.https.onRequest requires you to pass an HTTP request handler or an express app returned from express().

Here is the relevant article from Firebase about serving dynamic content from functions. https://firebase.google.com/docs/hosting/functions

Here is a video tutorial from Firebase on using express. https://www.youtube.com/watch?v=LOeioOKUKI8

jqualls
  • 1,483
  • 14
  • 19
0

To anyone looking for koa Google Cloud Functions, Here is my working version in typescript

import Koa from 'koa';
import Router from 'koa-router';
import type { HttpFunction } from '@google-cloud/functions-framework/build/src/functions';

const app = new Koa();

const port = process.env.PORT || 3001;

const router = new Router();

router.get('/', async (ctx) => {
  ctx.body = 'Hello World!';
});

app.use(router.routes());

// For development on local
if (!isCloudFunctions()) {
  app.listen(port, () => {
    console.log(`Server running on port ${port}`);
  });
}

export const helloWorldApi: HttpFunction = app.callback();

function isCloudFunctions(){
   return !!process.env.FUNCTION_SIGNATURE_TYPE;
}

For deployment:

gcloud functions deploy test-koa-function --entry-point=helloWorldApi --runtime nodejs16 --trigger-http --allow-unauthenticated
vanduc1102
  • 5,769
  • 1
  • 46
  • 43
-2

You can't deploy and run an arbitrary node app on Cloud Functions. You have to make use of the different types of triggers that are defined by the product.

See the Cloud Functions for Firebase main page to see the list.

  • Cloud Firestore Triggers
  • Realtime Database Triggers
  • Firebase Authentication Triggers
  • Google Analytics for Firebase Triggers
  • Crashlytics Triggers
  • Cloud Storage Triggers
  • Cloud Pub/Sub Triggers
  • HTTP Triggers
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    This is not the case for Firebase functions. Only if you're using Google Cloud Functions directly. – jqualls Mar 29 '18 at 17:58