Skip to content

Commit 957eaf9

Browse files
committed
update http event example
1 parent 8ab3632 commit 957eaf9

File tree

1 file changed

+31
-13
lines changed
  • module7-cloud-computing/r4.1-cloud-functions

1 file changed

+31
-13
lines changed

module7-cloud-computing/r4.1-cloud-functions/README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,37 @@ There are two distinct types of Cloud Functions: HTTP functions and event-driven
3333

3434
You invoke HTTP functions from standard HTTP requests. These HTTP requests wait for the response and support handling of common HTTP request methods like GET, PUT, POST, DELETE and OPTIONS. When you use Cloud Functions, a TLS certificate is automatically provisioned for you, so all HTTP functions can be invoked via a secure connection.
3535

36-
```js
37-
const escapeHtml = require("escape-html");
36+
This is an example using the Firebase SDK for Cloud Functions with an HTTPS trigger through building an endpoint returning the current time.
37+
The function date returns the current server date. You can pass it a format URL Query parameter to format the date.
3838

39-
/**
40-
* HTTP Cloud Function.
41-
*
42-
* @param {Object} req Cloud Function request context.
43-
* More info: https://expressjs.com/en/api.html#req
44-
* @param {Object} res Cloud Function response context.
45-
* More info: https://expressjs.com/en/api.html#res
46-
*/
47-
exports.helloHttp = (req, res) => {
48-
res.send(`Hello ${escapeHtml(req.query.name || req.body.name || "World")}!`);
49-
};
39+
```js
40+
exports.date = functions.https.onRequest((req, res) => {
41+
// Forbidding PUT requests.
42+
if (req.method === "PUT") {
43+
res.status(403).send("Forbidden!");
44+
return;
45+
}
46+
// [START usingMiddleware]
47+
// Enable CORS using the `cors` express middleware.
48+
cors(req, res, () => {
49+
// [END usingMiddleware]
50+
// Reading date format from URL query parameter.
51+
// [START readQueryParam]
52+
let format = req.query.format;
53+
// [END readQueryParam]
54+
// Reading date format from request body query parameter
55+
if (!format) {
56+
// [START readBodyParam]
57+
format = req.body.format;
58+
// [END readBodyParam]
59+
}
60+
// [START sendResponse]
61+
const formattedDate = moment().format(`${format}`);
62+
functions.logger.log("Sending Formatted date:", formattedDate);
63+
res.status(200).send(formattedDate);
64+
// [END sendResponse]
65+
});
66+
});
5067
```
5168

5269
## Background Functions
@@ -91,3 +108,4 @@ exports.helloPubSub = (message, context) => {
91108
- https://developers.google.com/learn/topics/functions
92109
- https://cloud.google.com/functions
93110
- https://aws.amazon.com/blogs/aws/introducing-cloudfront-functions-run-your-code-at-the-edge-with-low-latency-at-any-scale/
111+
- https://github.com/firebase/functions-samples

0 commit comments

Comments
 (0)