How to use express `res.sendFile()` in firebase cloud functions?

Viewed 1522

As per Firebase documentation, Cloud Functions need to end with res.send(), res.end() or res.redirect(). When I use res.sendFile() to send the content of a file, the console log show more than 1 invocation (actually 4-5 innovations).

So, what's proper way to use res.sendFile() in Firebase Cloud Functions?

1 Answers

It may be worth chaining a HTTP status code along with sendFile() to infer that the function has completed successfully, and therefore stop it from being invoked again:

res.status(200).sendFile(...)

The documentation on terminating HTTP functions provides an example with just send():

const formattedDate = moment().format(format);
console.log('Sending Formatted date:', formattedDate);
res.status(200).send(formattedDate);

But also the majority of the calls to send(), or other examples like json() within the Cloud Functions for Firebase Sample Library all use status() alongside.

Related