Firebase 9 / Heroku / KOA

by

in

Just documenting this process for reference…

Because I find that every time I do it, I have to search ffor the same things. So, I just wanted to put all the info in one place for necxt time I do it.

First step was getting setting up the Firebase project, and then getting the serviceAccountKey.json

Next, make sure you have heroku account, and login.

Here’s a link to getting started on heroku:

https://devcenter.heroku.com/articles/getting-started-with-nodejs

And here’s how to prep code for deploymen

https://devcenter.heroku.com/articles/preparing-a-codebase-ft:

So after:

npm init

and:

git init

And then installing these packages:

  "dependencies": {
    "@koa/cors": "^3.1.0",
    "@koa/router": "^10.1.1",
    "koa": "^2.13.4",
    "koa-bodyparser": "^4.3.0",
    "koa-static": "^5.0.0"
  }

I could then make a little ping app.

server.js

const Koa = require('koa');
const cors = require('@koa/cors');
const serve = require('koa-static');
const bodyParser = require('koa-bodyparser');


const router = require('./router.js');


const app = new Koa();

app
.use(cors())
.use(serve('./images'))
.use(bodyParser())
.use(router.routes());


app.listen(process.env.PORT || 5000, () => {
    console.log('Server started on Port 5000');
});

router.js

const Router = require('@koa/router');

const router = new Router();
router.get('/ping',ping);

function hello(ctx){
    ctx.body = {
        returnData:"Hello Curled 🥌"
    }
}

function ping(ctx){
    ctx.body = {
        returnJson:"You just got pinged!"
    }
}
module.exports = router;

When the ping app works:

Create a new app from the heroku dashboard with the name you want. Then grab that address and:

heroku git:remote -a example-app

Add a Procfile:

web: node server.js

Make sure app listens on the right port:

app.listen(process.env.PORT);

Do commits, and then:

git push heroku master

I wanted to have it on main instead of master as the modern naming convention everywhere else, but I’ll save that for another time. Just have to remember that cause I’m used to using main now.

Thats It!

Once you can ping the app live online, you can now develop locally knowing the production app is only a commit and a push away.

Now for firebase…

npm install firebase@9.4.1 --save
// Initialize Cloud Firestore through Firebase
import { initializeApp } from "firebase/app"
import { getFirestore } from "firebase/firestore"
const firebaseApp = initializeApp({
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
  projectId: '### CLOUD FIRESTORE PROJECT ID ###'
});

const db = getFirestore();

Converting require to import:

The alternative to:

 var Foo = require('foo');

is:

 import Foo from 'foo';

Forget everything I said about firebase!!!

Because it’s a node server application, I can just use firebase-admin package rather than separate web packages that represent different firebase versions (V8 or V9).

So I ended up :

npm uninstall firebase
npm install firebase-admin

Remove all the previous firebase code and add:

import {theKey} from './theKey.js';
import admin from 'firebase-admin';
import {getFirestore} from 'firebase-admin/firestore'
admin.initializeApp({
    credential: admin.credential.cert(theKey)
  });
  
const db = getFirestore();

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *