📖 Scenario: You are building a simple backend for a mobile app using Firebase. You want to create a Cloud Function that responds to HTTP requests and returns a welcome message.
🎯 Goal: Set up a Firebase Cloud Function project, write a basic HTTP function, and deploy it.
📋 What You'll Learn
Initialize a Firebase Cloud Functions project
Create an HTTP Cloud Function named helloWorld
The function should respond with the text 'Hello from Firebase!'
Deploy the Cloud Function
💡 Why This Matters
🌍 Real World
Cloud Functions let you run backend code without managing servers. This is useful for adding custom logic to your app, like sending notifications or processing data.
💼 Career
Many companies use serverless functions like Firebase Cloud Functions to build scalable backends quickly. Knowing how to set up and deploy these functions is a valuable skill for cloud developers.
Progress0 / 4 steps
1
Initialize Firebase Cloud Functions project
Run firebase init functions in your project folder and select JavaScript as the language. Then create a file named index.js with an empty exports object: const functions = require('firebase-functions'); and exports = {};
Firebase
Hint
Use firebase init functions to start the setup and create index.js with the required imports.
2
Add HTTP Cloud Function configuration
In index.js, add a new export named helloWorld that is a function using functions.https.onRequest. The function should take request and response parameters.
Firebase
Hint
Use exports.helloWorld = functions.https.onRequest((request, response) => { }) to define the function.
3
Implement the function response
Inside the helloWorld function, use response.send to send the text 'Hello from Firebase!' back to the client.
Firebase
Hint
Use response.send('Hello from Firebase!'); inside the function body.
4
Deploy the Cloud Function
Run firebase deploy --only functions:helloWorld in your terminal to deploy the helloWorld Cloud Function to Firebase.
Firebase
Hint
Use the Firebase CLI command firebase deploy --only functions:helloWorld to deploy your function.
Practice
(1/5)
1. What is the first command you run to start setting up Cloud Functions in a Firebase project?
easy
A. firebase init functions
B. firebase deploy --only functions
C. firebase start functions
D. firebase create functions
Solution
Step 1: Understand Firebase setup commands
The command firebase init functions initializes Cloud Functions in your project by creating necessary files and folders.
Step 2: Differentiate from deploy command
firebase deploy --only functions is used after setup to upload your functions to Firebase, not to start setup.
Final Answer:
firebase init functions -> Option A
Quick Check:
Setup starts with firebase init functions [OK]
Hint: Setup always begins with 'firebase init functions' command [OK]
Common Mistakes:
Confusing deploy command as setup
Using non-existent commands like 'firebase start functions'
Skipping initialization step
2. Which of the following is the correct way to deploy only Cloud Functions after writing your code?
easy
A. firebase deploy --only database
B. firebase deploy --only hosting
C. firebase deploy --only functions
D. firebase deploy --only storage
Solution
Step 1: Identify deployment targets
Firebase allows deploying specific parts like hosting, functions, database, or storage using the --only flag.
Step 2: Match the correct target for Cloud Functions
To deploy Cloud Functions only, use firebase deploy --only functions.
Final Answer:
firebase deploy --only functions -> Option C
Quick Check:
Deploy functions with --only functions [OK]
Hint: Use '--only functions' to deploy just Cloud Functions [OK]
Common Mistakes:
Using wrong target like hosting or database
Omitting '--only' and deploying everything
Typing incorrect command syntax
3. Given this Cloud Function code snippet, what will happen when an HTTP request is made to it?
Step 1: Identify the correct trigger for new Firebase Authentication users
The trigger functions.auth.user().onCreate runs when a new user is created in Firebase Authentication.
Step 2: Confirm the logging of user email
The callback receives a user object with an email property, which can be logged directly.
Step 3: Differentiate from other triggers
Database triggers listen to database changes, HTTP triggers respond to requests, and storage triggers respond to file changes, so they are incorrect here.
Final Answer:
Use auth user onCreate trigger to log new user email. -> Option D
Quick Check:
Auth user onCreate triggers new user events [OK]
Hint: Use auth.user().onCreate for new Firebase Authentication users [OK]
Common Mistakes:
Using database triggers for auth events
Using HTTP triggers to detect user creation
Confusing storage triggers with auth triggers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.