The Simplest Backend for my App — The Senior Newbie Quests

  • generous free tier and scalable paid one;
  • tedious features to setup are available out-of-the-box — e.g.: users and their authentication, push notifications, etc.;
  • remote logic — e.g.: cloud functions;
  • platform specific APIs.

Preliminary steps

  1. Register to Firebase with your Google account.
  2. Create a new project on Firebase Console.
  3. At this point you have already access to SDKs, database, user authentication, etc. with no costs.

Going deeper

  • a function can be triggered by a platform event — e.g.: a database record is inserted and you want to fire a push notification;
  • you can call a function via SDK or HTTP request;
  • you can exploit HTTP features to host Node.js apps — at this time Node.js 6 is officially supported and Node.js 8 is in beta.
$ brew install node
$ npm install -g firebase-tools
$ cd your/development/folder
$ mkdir my-project-name
$ cd my-project-name
$ firebase init hosting
$ firebase init functions
Project structure: in functions you have dynamic content; in public you can put static files. Please note that, if you use version control — e.g.: Git — you may want to exclude node_modules path.

Installing Express.js

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

$ cd functions
$ npm install express firebase-admin@latest firebase-functions@latest --save
$ cd functions
$ npm run serve
If you try to browse to the local web server you will get an error: this is normal, because you still need to setup routes.

Adding routes

This means: for the HTTP verb GET, for path /, send as response this string.
MIME Type is text/html
MIME Type is application/json
Results of calling […]/app/hello/Marco

Using cache

  • publicindicates that the response may be cached by any cache.
  • max-agespecifies the maximum amount of time (in seconds) this resource will be stored in user's browser.
  • s-maxage: for a shared cache (but not for a private cache), the maximum age specified by this directive overrides the maximum age specified by either the max-agedirective or the Expiresheader. So it says how long this resource will be stored in the CDN.

Render views

$ cd functions
$ npm install handlebars consolidate --save
Results of calling […]/app/goodbye/Moon

Integrating with Firebase features

Deploying to Firebase server

$ firebase deploy
Our Node.js 8 serverless API is ready and live

Sources

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marco Muccinelli

Marco Muccinelli

Call me Muccy. I’m a Senior iOS Developer.