An HTTP trigger function is a cloud function that runs when it receives an HTTP request. It lets you handle web requests like a web server.
Click to reveal answer
beginner
How do you define an HTTP trigger function in Firebase?
You define it using functions.https.onRequest((req, res) => { ... }). This sets up a function that runs when an HTTP request is made.
Click to reveal answer
beginner
What are the two main objects passed to an HTTP trigger function handler?
The two objects are req (request) and res (response). req has info about the incoming request, and res is used to send back a response.
Click to reveal answer
intermediate
Why would you use an HTTP trigger function instead of a regular Firebase function?
Because HTTP trigger functions let you create APIs or webhooks that respond to web requests directly, making your backend accessible over the internet.
Click to reveal answer
beginner
How do you send a JSON response from an HTTP trigger function?
Use res.json({ key: 'value' }) to send a JSON object back to the client.
Click to reveal answer
Which method defines an HTTP trigger function in Firebase?
Afunctions.database.onWrite()
Bfunctions.auth.user().onCreate()
Cfunctions.https.onRequest()
Dfunctions.storage.onFinalize()
✗ Incorrect
Only functions.https.onRequest() sets up an HTTP trigger function.
What does the res object do in an HTTP trigger function?
AContains info about the incoming request
BSends a response back to the client
CTriggers the function execution
DStores data in the database
✗ Incorrect
res is used to send back responses like text or JSON.
How do you send a plain text response in an HTTP trigger function?
Areq.send('text')
Bres.json('text')
Creq.json('text')
Dres.send('text')
✗ Incorrect
res.send() sends plain text or HTML responses.
Which of these is NOT a typical use case for HTTP trigger functions?
AResponding to database changes
BServing dynamic web content
CHandling webhooks
DCreating REST APIs
✗ Incorrect
Responding to database changes uses database triggers, not HTTP triggers.
What happens if you don't send a response in an HTTP trigger function?
AThe function times out and the client waits indefinitely
BThe function automatically sends a default response
CThe function logs an error but returns success
DThe function restarts automatically
✗ Incorrect
Without sending a response, the client waits until the function times out.
Explain how an HTTP trigger function works in Firebase and what objects it uses.
Think about how a web server handles requests and responses.
You got /5 concepts.
Describe a simple example of when you would use an HTTP trigger function in a Firebase project.
Imagine you want to let a website or app ask your backend for data.
You got /4 concepts.
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?