diff --git a/functions/README.md b/functions/README.md index 02f526e4a8..f604a8b184 100644 --- a/functions/README.md +++ b/functions/README.md @@ -24,6 +24,7 @@ environment. * [Cloud Storage](gcs/) * [Cloud Datastore](datastore/) * [Cloud Pub/Sub](pubsub/) -* [Dependencies](uid/) +* [Dependencies](uuid/) +* [HTTP](http/) * [Logging](log/) * [Modules](module/) diff --git a/functions/http/README.md b/functions/http/README.md new file mode 100644 index 0000000000..fc2f61eb91 --- /dev/null +++ b/functions/http/README.md @@ -0,0 +1,35 @@ +Google Cloud Platform logo + +# Google Cloud Functions HTTP sample + +This recipe shows you how to respond to HTTP requests with a Cloud Function. + +View the [source code][code]. + +[code]: index.js + +## Deploy and Test + +1. Follow the [Cloud Functions quickstart guide][quickstart] to setup Cloud +Functions for your project. + +1. Clone this repository: + + git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git + cd nodejs-docs-samples/functions/http + +1. Create a Cloud Storage Bucket to stage our deployment: + + gsutil mb gs://[YOUR_BUCKET_NAME] + + * Replace `[YOUR_BUCKET_NAME]` with the name of your Cloud Storage Bucket. + +1. Deploy the "helloGET" function with an HTTP trigger + + gcloud alpha functions deploy publish --bucket [YOUR_BUCKET_NAME] --trigger-http + +1. Call the "helloGET" function: + + curl https://[YOUR_PROJECT_REGION].[YOUR_PROJECT_ID].cloudfunctions.net/helloGET + +[quickstart]: https://cloud.google.com/functions/quickstart diff --git a/functions/http/index.js b/functions/http/index.js new file mode 100644 index 0000000000..3daec9cfff --- /dev/null +++ b/functions/http/index.js @@ -0,0 +1,43 @@ +// Copyright 2016, Google, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Responds to a GET request. + * + * @example + * gcloud alpha functions call helloGET + * + * @param {Object} req Cloud Function request context. + * @param {string} req.method HTTP method of the request. + * @param {Object} res Cloud Function response context. + * @param {Function} res.send Write data to the response stream. + * @param {Function} res.status Set the status of the response. + */ +function helloGET (req, res) { + console.log(req.method); + switch (req.method) { + case 'GET': + res.send('Hello World!'); + break; + case 'POST': + res.status(403).send('Forbidden!'); + break; + default: + res.status(500).send({ error: 'Something blew up!' }); + break; + } +} + +exports.helloGET = helloGET; diff --git a/functions/log2/index.js b/functions/log2/index.js index a1f7b20439..9413ff3f23 100644 --- a/functions/log2/index.js +++ b/functions/log2/index.js @@ -21,8 +21,8 @@ exports.helloworld = function (context, data) { // [END walkthrough_pubsub] // [START walkthrough_http] -exports.hellohttp = function (context, data) { - // Use the success argument to send data back to the caller - context.success('My GCF Function: ' + data.message); +exports.hellohttp = function (req, res) { + // Use the response argument to send data back to the caller + res.send('My GCF Function: ' + req.body.message); }; // [END walkthrough_http]