Cloud Function Example

Cloud function example

Create function


Edit default function 




helloworld.js

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.helloWorld = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};

Go to testing tab




Test the function via URL using trigger tab


Hello World Function


For example 2 lets create a google cloud storage bucket which we will use for our assignment number 2




Create a bucket


I called mine uconn-customer


Make it public

click on permissions

Add members allUsers and Role Storage Object Viewer



The bucket is now ready to load web pages.



Httptest function example



Create function called httptest to take an html page with input and send back to user



delete the sample code and insert new code in the inline editor

and hit create (deploy if you already created and it needs editing)

make sure the exports.httptest matches the function name to execute on bottom of screen




httpstest.js

exports.httptest = (req, res) => {
  let email = req.body.myemail;
  let name = req.body.myname;
  let response = "<!DOCTYPE html><html><h1>You Entered</h1><br><p>Message: " + email + name +" <br> ";

  res.status(200).send(response);
};

*** Note code will parse the email and name fields from the html body of the requested page
*** Formatting the response to the page request with a set of HTML commands to let browser understand and format the response

Click back into the function




Click on trigger tab





Copy the url for your web page



Web page link for email and name


Next select the trigger for putting in your html

use a local editor like notepad and create a file called httptest.html


the trigger link will be executed when the form action posts by a user hitting the submit button

once this file is saved you will need to upload it to your storage drive

make sure it is save as html

<html>

<h1>

</h1>

<h1>http test Information</h1>

<br>

<br>

<form method="POST" action="https://us-central1-tenacious-cocoa-267203.cloudfunctions.net/httptest">

Email: <input type="text" name="myemail" size="35"><br>

Name: <input type="text" name="myname" size="35"><br>

<input type="submit" value="Submit form">

</form>

</html>




now that the file is saved we use the storage upload to make it accessible

Upload my file httptest.html







Click on link to load page


Web page link for email and name

No comments:

Post a Comment

Office hours tomorrow(Tuesday) 5:00pm-6:00pm, 4/26/2021, 5:13 PM, English, 4/26/2021, 5:13 PM

Your assigned language is: English Classroom blog: googleclouduconn.blogspot.com 4/26/2021, 5:13 PM Office hours tomorrow(Tuesday) 5...