Setting up Cloud Functions

In notebook:
FrontEndMasters Serverless
Created at:
2017-10-15
Updated:
2017-10-15
Tags:
backend JavaScript

firebase.json

https://github.com/stevekinney/hot-takes-redux/blob/live-coding/firebase.json

{
  "database": {
    "rules": "database.rules.json"
  },
  "hosting": {
    "public": "build",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

functions/package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "dependencies": {
    "firebase-admin": "^4.1.2",
    "firebase-functions": "^0.5"
  },
  "private": true
}
  //	****		functions/index.js		****

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.shushYellers = functions.database.ref('/messages/{message}/content').onWrite(event => {
  const data = event.data;
  const content = data.val();
  if (content && content === content.toUpperCase()) {
    data.ref.set(content.toLowerCase());
  }
});