GCP101 Chapter 12 Using Google Cloud Functions

Previous chapter | TOC | GCP | Next chapter

© Copyright 2018 Nickantony Quach. All rights reserved.

1 Overview

In this chapter, you will be asked to create a new cloud service, which is a function residing, not on your local machine but, in the cloud using the facility known as the Google Cloud Functions.

The name of the new cloud service will be mymainmenu. Its job is to generate a web page that has a list of links as shown in the following image.

2 Getting Started

After you sign in with GCP, use the GCP Shortcuts at Ri4C to access the List of Google Cloud Functions.

3 Creating a new cloud service

Do the following to create your first cloud service:

  1. Sign in with GCP.
  2. Go to the List of Google Cloud Functions.
  3. Click on the [+] to CREATE FUNCTION.
  4. Set name as “mymainmenu” (without quotes).
  5. Replace the content of index.js with the code in Appendix A below.
  6. Set function to execute as “myMainMenu” (case sensitive).
  7. Click on the button Create to create a new cloud service as specified.
  8. Wait for GCP to deploy “mymainmenu”, the newly created cloud service.
  9. Click on the name to open the function “mymainmenu”.
  10. Click on the tab Trigger.
  11. Click on the URL to trigger the service, which generates the simple web page, My Main Menu.

4 Introducing HTML

At this time, you must not understand the syntax used in the source code. However, here is how its logic works.

  • A program can tell your browser to display a link by sending it a text string using syntax of the anchor tag of HTML. Click here for detail information on the HTML a tag.
  • A program can tell your browser to display a list item by sending it a text string using syntax of the list-item tag of HTML. Click here for detail information on the HTML li tag.
  • A program can tell your browser to display an unordered list by sending it a text string using syntax of the unordered-list tag of HTML. Click here for detail information on the HTML ul tag.
  • A program can tell your browser to display a topic header by sending it a text string using syntax of the heading tag of HTML. Click here for detail information on the HTML h-tag.

It is not necessary but we can enclose the entire code between the HTML tag for start of document and the HTML tag for end of document. Click here for detail information on the document tag HTML.

5 Introducing Node.js

Node.js is an open-source, cross-platform JavaScript run-time environment for executing JavaScript code server-side. Historically, JavaScript was used primarily for client-side scripting, in which scripts written in JavaScript are embedded in a webpage’s HTML, to be run client-side by a JavaScript engine in the user’s web browser. Node.js enables JavaScript to be used for server-side scripting, and runs scripts server-side to produce dynamic web page content before the page is sent to the user’s web browser. Tell me more

At this time, you must not understand the syntax used in the source code. However, here is how its logic works.

  1. The first keyword used in the following program is exports. Click here for detail information on what it is for and how to use it.
  2. The second line of code with the keywords status and send are used to send the HTML code to the receiving browser. Click here for detail information on the syntax of this code.

Appendix A Code for index.js

At this time, you must not understand the syntax used in the following source code.

exports.myMainMenu = (req, res) => {

res.status(400).send(`<h3>My Main Menu</h3><ul>

<li><a href="https://ri4c.com/gcp/" target="_blank">GCP Shortcuts at Ri4C</a></li>
<li><a href="https://ri4c.com/gcp101" target="_blank">GCP 101 by Ri4C</a></li>

</ul>`);

};

// Origin: https://ri4c.com/gcp101-chapter-12-using-google-cloud-functions/

*** End of Chapter ***

Next chapter


© Copyright 2018 Nickantony Quach. All rights reserved. | Other books | Edit