This repo contains the code for a "serverless" backend using Google Firebase Cloud Functions and FireStore database. It's incredibly simple and just acts as a CRUD app with defined endpoints and database changes.
Since this setup is "serverless" you can't simply run npm start
or npm run start
and browse to a localhost. Instead do the following:
- Make sure you have firebase tools installed:
npm install -g firebase-tools
- Install package dependencies within the functions folder:
cd functions && npm install
- Emulate firebase locally:
serve
To deploy simply use: deploy
From here it's possible to hit an endpoint using Or by using Postman or curl
(assuming it's not under auth):
curl -G "urlReturnedByDeployCommandAbove/api/doc" #GET
curl -d '{"data": "object"}' -H "Content-Type: application/json" -X POST "endpointURL" #POST
curl -X DELETE "endpointURLForSpecificItem" #DELETE
functions/package.json
- defines dependencies andnpm
commands for running the app (likerequirements.txt
in python)functions/package-lock.json
- a more detailed version of the above, designed to be put under version controlfunctions/index.js
- core setup of the app (e.g. registering endpoints, overall authentication handling)functions/model.js
- defines the database CRUD operations (create, read, update, delete) for different Firestore collectionsfunctions/controller.js
- logic for each endpoint defined inindex.js
firebase.json
- config file for what happens when the app is deployed to Firebasepublic/*
- autogenerated html templates
dotenv
dontenv allows us to load app configuration files from a.env
file in the root of the app directoryexpress
express is the core framework for server and route creation (like Flask or Sanic)firebase
firebase is for database read/write operationsfirebase-admin
firebase-admin is for database configuration and user authorizationbabel
andbabel-preset-env
these packages are for "transpiling", i.e. allowing us to write javascript using the most modern syntax (e.g.import
) while allowing compatibility with various node versions that may not support the new syntax