📖 Scenario: You are building a small web service using Firebase Functions. Your service will respond to HTTP requests and return simple messages. This is useful for creating APIs or webhooks.
🎯 Goal: Build a Firebase HTTP trigger function that responds with a greeting message. You will set up the function, configure a message, write the core logic to handle the HTTP request, and complete the function export.
📋 What You'll Learn
Create a Firebase function named helloWorld that triggers on HTTP requests.
Add a configuration variable greeting with the value 'Hello from Firebase!'.
Write the function logic to send the greeting message as the HTTP response.
Export the helloWorld function properly for Firebase to recognize it.
💡 Why This Matters
🌍 Real World
HTTP trigger functions are used to create APIs, webhooks, or simple web services that respond to internet requests.
💼 Career
Understanding how to write and deploy HTTP trigger functions is essential for backend developers working with serverless platforms like Firebase.
Progress0 / 4 steps
1
Set up the Firebase HTTP function
Write the first line to import functions from firebase-functions and create a Firebase HTTP function named helloWorld using functions.https.onRequest with an empty callback function that takes request and response parameters.
Firebase
Hint
Start by importing firebase-functions and define helloWorld as an HTTP trigger function with empty parameters.
2
Add a greeting message variable
Add a constant variable named greeting and set it to the string 'Hello from Firebase!' just before the exports.helloWorld line.
Firebase
Hint
Define greeting as a constant string before the function export.
3
Send the greeting in the HTTP response
Inside the helloWorld function, use response.send(greeting) to send the greeting message as the HTTP response.
Firebase
Hint
Use response.send() inside the function to return the greeting.
4
Complete the Firebase HTTP function export
Ensure the entire code exports the helloWorld function correctly with the greeting response inside the HTTP trigger callback.
Firebase
Hint
Check that the function is exported and sends the greeting message properly.
Practice
(1/5)
1. What does an HTTP trigger function in Firebase do?
easy
A. Automatically updates the database without user input
B. Runs code when a specific web URL is visited
C. Sends emails to users without any request
D. Creates a new Firebase project
Solution
Step 1: Understand HTTP trigger functions
HTTP trigger functions run when someone visits a specific web address (URL).
Step 2: Compare options with this behavior
Only Runs code when a specific web URL is visited describes running code on visiting a URL, matching the definition.
Final Answer:
Runs code when a specific web URL is visited -> Option B
Quick Check:
HTTP trigger = run code on URL visit [OK]
Hint: HTTP trigger = runs code on web URL visit [OK]
Common Mistakes:
Confusing HTTP triggers with database triggers
Thinking HTTP triggers run automatically without requests
Assuming HTTP triggers send emails by default
2. Which of the following is the correct way to define an HTTP trigger function in Firebase using Node.js?
easy
A. exports.myFunc = functions.storage.object().onFinalize((object) => { });
B. exports.myFunc = functions.database.onWrite((change, context) => { });
C. exports.myFunc = functions.https.onRequest((req, res) => { res.send('Hello'); });
D. exports.myFunc = functions.auth.user().onCreate((user) => { });
Solution
Step 1: Identify HTTP trigger syntax
HTTP triggers use functions.https.onRequest with (req, res) parameters.
Step 2: Match options to this syntax
Only exports.myFunc = functions.https.onRequest((req, res) => { res.send('Hello'); }); uses functions.https.onRequest correctly; others are for database, auth, or storage triggers.
response.send() ends the HTTP response. Calling it twice causes an error.
Step 2: Analyze the code flow
The function calls response.send('Test') then immediately calls response.send('Done'), which is invalid.
Final Answer:
Calling response.send() twice causes an error -> Option D
Quick Check:
Only one response.send() allowed per request [OK]
Hint: Use response.send() only once per HTTP request [OK]
Common Mistakes:
Calling response.send() multiple times
Thinking parameter names must be req, res exactly
Ignoring that response ends after send
5. You want to create an HTTP trigger function that accepts a JSON body with a field age and responds with {"canVote": true} if age is 18 or more, otherwise {"canVote": false}. Which code snippet correctly implements this?