Setup Google/Firebase Cloud Functions Using Node

February 14, 2020

Create Firebase Project

Setup Node.js Environment

NOTE: This tutorials setup Firebase Functions without Firebase Hosting. If you wanted both, refer to Setup Firebase Hosting and Functions (Node).

Install Firebase CLI

npm install -g firebase-tools

Setup Firebase CLI

firebase login

Create project directory.

mkdir PROJECT
cd PROJECT

Setup Cloud Functions

firebase init functions
What language would you like to use to write Cloud Functions? JavaScript
? Do you want to use ESLint to catch probable bugs and enforce style? No
✔  Wrote functions/package.json
✔  Wrote functions/index.js
✔  Wrote functions/.gitignore
? Do you want to install dependencies with npm now? Yes

Create Cloud Functions

cd functions

Edit index.js.

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

exports.hello = functions.https.onRequest(async (request, response) => {
  const name = request.query.name;
  response.send(`Hello ${name}`)
})

Test Local Development

npm run serve

Access: http://localhost:5000/PROJECT/us-central1/hello?name=Desmond

Deploy

npm run deploy

or

firebase deploy --only functions

Deploy specific functions only

firebase deploy --only functions:hello

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.