Porting your regular Express app to Firebase cloud functions in 5 minutes

Disclaimer: hyperbolic statements and over ambitious time estimations might occur. Real time to implement may vary. Awesomeness is guaranteed!

Dennis Alund
oddbit
3 min readSep 26, 2017

--

Let me just head you in the right direction first, to watch David East’s awesome video on how to serve an Express app with cloud functions… and actually, that can be the end of the story. You can stop reading after watching that if you want. But in the article that follows below the video, I’ll showcase a real example on how I took a Node/Express app and ported it to Firebase with minimal configuration changes (and no changes at all to the actual app).

The project

As anyone who has studied at university, you know that you should start with your problem description.

My situation is that I am running a coworking space and managing a wifi for the members with Unifi access points. I wanted to have a setup where their network access was in sync with their membership status in our management system (we’re using a system called Nexudus, for further reference below). Not a very mainstream problem, I know.

If you were to imagine what the application workflow, it would look something like this.

  1. Client device is connecting to wifi that is managed by the hotspot.
  2. Access point is redirecting the connection request or opening a URL to the hotspot login. The visitor is prompted to login with his/her Nexudus account.
  3. The account credentials are validated against Nexudus and active membership status is checked.
  4. An active membership will send back a request to activate a hotspot session for the MAC address of the connecting device.

Justification

So what was the real reason here? Does it actually solve a real world problem? I’d say like it was more like this:

If you think that you could host your hotspot portal on Firebase, you should.

The project actually started off because I figured it was about time for me to learn Docker. But as I wasn’t really happy with the fact that I still need to run that container somewhere, I thought that I could do better.

How easy is it to take a project developed as a regular Node/Express app to run in a Docker container, and to port it to Firebase hosting/cloud functions?

Turns out that it was very easy. The whole project is open sourced and works well as a proof of concept. Although it’s afully functional as a hotspot portal (let me know if something isn’t), it might not be addressing all your needs.

It should be fairly simple for you to get the general idea of how to adopt it to your own backend or system integration.

The configuration

The first thing that I needed to do, was to separate the configuration so that it was easier to launch the application on different platforms.

The application bootstrapping is implemented in app.js and the application’s entry points are either server.js for running as a regular Node application and index.js for Firebase cloud function deployment.

Before Firebasing the app (yes, it’s almost a word) I could rely on all configuration to be set in ENV variables. But with Firebase I needed to consider the Firebase functions environment configuration.

So the different entry points will take care of the platform specific ways of exposing the application’s entry point and how to access its environment configuration.

The application

Apart from the configuration, it’s all the same application as it was before porting it to Firebase. The only thing that changed was the entry point and configuration of the application.

The result is that the application is able to be distributed as both a fully managed version on Firebase hosting and as a self managed Docker image with zero effort of maintaining different code bases.

The conclusion

Managing wifi network access with Firebase is pretty damn awesome.

--

--

Dennis Alund
oddbit
Editor for

Google Developer Expert for Firebase, nerd and passionate problem solver | Founder of Kumpul coworking space in Bali