-
Notifications
You must be signed in to change notification settings - Fork 2k
Simplify function doc samples #717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
3d29d13
1d381cb
9e0143c
1723fdc
91725be
debcc07
d46cc6f
15cca2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,14 +23,14 @@ | |
| * @param {Object} res ExpressJS object containing the HTTP response to send. | ||
| */ | ||
| exports.helloWorld = (req, res) => { | ||
| if (req.body.message === undefined) { | ||
| if (req.body.message) { | ||
| // This is an error case, as "message" is required | ||
| res.status(400).send('No message defined!'); | ||
| } else { | ||
| // Everything is ok - call request-terminating method to signal function | ||
| // completion. (Otherwise, the function may continue to run until timeout.) | ||
| console.log(req.body.message); | ||
| res.status(200).end(); | ||
| return res.status(400).send('No message defined!'); | ||
| } | ||
| // Everything is ok - call request-terminating method to signal function | ||
| // completion. (Otherwise, the function may continue to run until timeout.) | ||
| console.log(req.body.message); | ||
| res.status(200).end(); | ||
| } | ||
| }; | ||
| // [END functions_http_helloworld] | ||
|
|
@@ -44,41 +44,28 @@ exports.helloWorld = (req, res) => { | |
| * @param {Object} res Cloud Function response context. | ||
| */ | ||
| exports.helloContent = (req, res) => { | ||
| let name; | ||
|
|
||
| switch (req.get('content-type')) { | ||
| // '{"name":"John"}' | ||
| case 'application/json': | ||
| name = req.body.name; | ||
| break; | ||
|
|
||
| let name = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jmdobry @MylesBorins I like this pattern - is it sufficiently idiomatic?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see req.body being explicitly set/unset by Cloud Functions on the
way to HTTP function. According to
https://expressjs.com/en/api.html#req.body:
'By default, it is undefined, and is populated when you use body-parsing
middleware such as body-parser and multer.'
Cloud Functions does use body-parser so I assume req.body should always be
set.
…On Fri, Aug 31, 2018 at 11:00 AM Ace Nassri ***@***.***> wrote:
In functions/http/index.js
<#717 (comment)>
:
> @@ -44,41 +44,28 @@ exports.helloWorld = (req, res) => {
> * @param {Object} res Cloud Function response context.
> */
> exports.helloContent = (req, res) => {
> - let name;
> -
> - switch (req.get('content-type')) {
> - // '{"name":"John"}'
> - case 'application/json':
> - name = req.body.name;
> - break;
> -
> + let name = {
One thing to bear in mind: *all* values of this dictionary will be
evaluated upon dictionary creation - and *all* must succeed, regardless
of the Content-Type of the incoming request.
(Namely: this technique will fail if req.body is undefined. @swalkowski
<https://github.com/swalkowski>, is such a thing possible?)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://github.com/GoogleCloudPlatform/nodejs-docs-samples/pull/717/files/3d29d13d27763d7820936a388f879a4b17605379#r214432230>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AL7pgtnBTLaaAJ7AMT4PrhY6ZgnZq1CNks5uWXmwgaJpZM4WIHnA>
.
|
||
| // '{"name":"John"}' | ||
| 'application/json': req.body.name, | ||
| // 'John', stored in a Buffer | ||
| case 'application/octet-stream': | ||
| name = req.body.toString(); // Convert buffer to a string | ||
| break; | ||
|
|
||
| 'application/octet-stream': req.body.toString(), // Convert buffer to a string | ||
| // 'John' | ||
| case 'text/plain': | ||
| name = req.body; | ||
| break; | ||
|
|
||
| 'text/plain': req.body, | ||
| // 'name=John' in the body of a POST request (not the URL) | ||
| case 'application/x-www-form-urlencoded': | ||
| name = req.body.name; | ||
| break; | ||
| } | ||
| 'application/x-www-form-urlencoded': req.body.name; | ||
| }[req.get('content-type')]; | ||
|
|
||
| res.status(200).send(`Hello ${name || 'World'}!`); | ||
| }; | ||
| // [END functions_http_content] | ||
|
|
||
| // [START functions_http_method] | ||
| function handleGET (req, res) { | ||
| function handleGET(req, res) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert this. This is enforced by our linter ( |
||
| // Do something with the GET request | ||
| res.status(200).send('Hello World!'); | ||
| } | ||
|
|
||
| function handlePUT (req, res) { | ||
| function handlePUT(req, res) { | ||
| // Do something with the PUT request | ||
| res.status(403).send('Forbidden!'); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing an
!?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.