Consider this Firebase HTTP trigger function code snippet:
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send('Hello from Firebase!');
});What will the client receive when making a GET request to this function?
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send('Hello from Firebase!');
});Look at what the response.send() method sends back.
The response.send() method sends the exact string as the HTTP response body. Here, it sends 'Hello from Firebase!'.
Which option contains the correct syntax for defining a Firebase HTTP trigger function?
exports.myFunction = functions.https.onRequest((req, res) => {
res.send('Hi');
});Check the arrow function syntax and parentheses.
The correct syntax uses functions.https.onRequest((req, res) => { ... }) with parentheses around parameters and the arrow function inside.
You want your Firebase HTTP trigger function to respond quickly without waiting for asynchronous tasks. Which option achieves this?
exports.fastResponse = functions.https.onRequest(async (req, res) => { // Some async task await someAsyncTask(); res.send('Done'); });
Think about sending the response without waiting for the async task.
Removing await lets the function send the response immediately, improving response time. The async task runs in the background.
If a Firebase HTTP trigger function does not call response.send() or similar methods, what will happen?
Think about what happens when a server never replies to a request.
If no response is sent, the client waits until the request times out, causing a hanging request.
Examine this function:
exports.timeoutFunc = functions.https.onRequest((req, res) => {
someAsyncOperation();
// Missing response.send or end
});Why does this cause a timeout error?
Check if the function sends any response back to the client.
The function calls an async operation but never sends a response. This causes the client to wait until timeout.