Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
35 changes: 35 additions & 0 deletions functions/http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>

# 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
43 changes: 43 additions & 0 deletions functions/http/index.js
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 3 additions & 3 deletions functions/log2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]