NodeJS REST API with authentication (Using express and firebase for storage and authentication)

Introduction

For the backend of our application we created a NodeJS server which serves as a platform.

  • Make it possible for users to configure their home in the APP
  • Make it possible for admins to add electronics/components to our backend
  • Make it possible for users to view their home and connected components.
  • Make it possible for user to get a component’s history
  • Trigger (timed) actions for components
  • Users get real-time updates from their home.
  • Users can manipulate the components from the APP

Basic configuration

Packages

Set up a google firebase account

  1. Login in firebase with your google account. If you don’t have one yet you can sign up here
  2. Go to console
  3. Add a new project (for testing purposes you can turn off google analytics)
  4. Go to Project Setting → Service Accounts
  5. Create a service account and click “Generate new private key”. Make sure to store this file in a save place and also add it to your .gitignore if your repo is public.
  6. Go back to the project main page and click the “</>” (web) symbol.
  7. Give your app a name and copy the firebaseConfig variable. Make sure to save this config in a file within your project.

Architecture

  • controllers: Where all the controllers are. All controllers have the same parameters e.g request and response.
  • middleware: All middleware will go here.
  • model: The model folder is obsolete but is helpful for knowing which attributes there are for certain models.
  • repository: This folder will contain all the repositories that will request and or persist data to firebase
  • service: Main business logic is here.
  • utils: Some utility functions go here.

Express

If you want to get the same pipeline like Java Spring or .NET Core then you will need to configure this yourself or use express (try to use the latest version or atleast 4.17.1 as old versions of express didn’t come with a basic request pipeline)

Routing

We need to tell express where to send the requests. We do this in app/config/routes.

Firebase config

For our server we need to use 2 versions of firebase: Firebase SDK and the Firebase Admin SDK. Both need to be configured and separated from another.

MQTT

Because we are building a home automation platform/service we will need to talk to electronic components like sensors and/or lights. For this we will use MQTT to make sure that our messages will get delivered to the home. We opted for a free online broker: emqx

Websockets (Socket_IO)

We use websockets to send updates to our frontend application.

  • The socket_io version have to be the same in both frontend and backend
  • The websocket needs to be run on a different port than the API.

Conclusion

Working with NodeJs is a completely new experience if you’re coming from a typed language like Java & .NET . A lot of times things don’t work because you used the wrong attribute name, forgot to await a promise (it happens),using the wrong library version,… . This was a refreshing experience and even when having some hardships with the used libraries, it was really fun.