Your First Function: Node.js

This guide takes you through the process of writing a Cloud Function using the Node.js runtime. There are two types of Cloud Functions:

  • An HTTP function, which you invoke from standard HTTP requests.
  • A background function, which you use to handle events from your Cloud infrastructure, such as messages on a Cloud Pub/Sub topic, or changes in a Cloud Storage bucket.

The sample shows how to create a simple HTTP function.

Guide structure

  1. Creating a GCP project using Cloud SDK
  2. Creating a function
  3. Specifying dependencies
  4. Deploying your function
  5. Testing your function

Creating a GCP project using Cloud SDK

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to the project selector page

  3. Zorg dat facturering is ingeschakeld voor uw project.

    Meer informatie over het inschakelen van facturering

  4. Enable the Cloud Functions API.

    Enable the API

  5. Install and initialize the Cloud SDK.
  6. Update and install gcloud components:
    gcloud components update
  7. Need a command prompt? You can use the Google Cloud Shell. The Google Cloud Shell is a command line environment that already includes the Google Cloud SDK, so you don't need to install it. The Google Cloud SDK also comes preinstalled on Google Compute Engine Virtual Machines.

  8. Prepare your development environment.

    Go to the Node.js setup guide

Creating a function

  1. Create a directory on your local system for the function code:

    Linux or Mac OS X

    mkdir ~/gcf_hello_world
    cd ~/gcf_hello_world
    

    Windows

    mkdir %HOMEPATH%\gcf_hello_world
    cd %HOMEPATH%\gcf_hello_world
    
  2. Create an index.js file in the gcf_hello_world directory with the following contents:

    const escapeHtml = require('escape-html');
    
    /**
     * HTTP Cloud Function.
     *
     * @param {Object} req Cloud Function request context.
     *                     More info: https://expressjs.com/en/api.html#req
     * @param {Object} res Cloud Function response context.
     *                     More info: https://expressjs.com/en/api.html#res
     */
    exports.helloHttp = (req, res) => {
      res.send(`Hello ${escapeHtml(req.query.name || req.body.name || 'World')}!`);
    };

    This example function takes a name supplied in the HTTP request and returns a greeting, or "Hello World!" when no name is supplied.

Specifying dependencies

Dependencies in Node.js are managed with npm and expressed in a metadata file called package.json.

  1. Create a package.json file in the gcf_hello_world directory, either manually or by running the command:

    npm init
    
  2. Add the function's dependency, in this case the escape-html package, to your package.json file, either manually or by running the command:

    npm install escape-html
    

    Your package.json file should contain a section like the following:

    {
      ...
      "dependencies": {
        "escape-html": "^1.0.3"
      }
      ...
    }
    

Deploying the function

To deploy the function with an HTTP trigger, run the following command in the gcf_hello_world directory:

gcloud functions deploy helloHttp --runtime nodejs8 --trigger-http

Testing the function

  1. When the function finishes deploying, take note of the httpsTrigger.url property or find it using the following command:

    gcloud functions describe helloHttp
    

    It should look like this:

    https://GCP_REGION-PROJECT_ID.cloudfunctions.net/helloHttp
  2. Visit this URL in your browser. You should see a "Hello World!" message.

    Try passing a name in the HTTP request, for example by using the following URL:

    https://GCP_REGION-PROJECT_ID.cloudfunctions.net/helloHttp?name=NAME

    You should see the message "Hello NAME!"

Viewing logs

Using the command-line tool

Logs for Cloud Functions are viewable in the Stackdriver Logging UI, and via the gcloud command-line tool.

To view logs for your function with the gcloud tool, use the logs read command, followed by the name of the function:

gcloud functions logs read helloHttp

The output should resemble the following:

LEVEL  NAME       EXECUTION_ID  TIME_UTC                 LOG
D      helloHttp  rvb9j0axfclb  2019-09-18 22:06:25.983  Function execution started
D      helloHttp  rvb9j0axfclb  2019-09-18 22:06:26.001  Function execution took 19 ms, finished with status code: 200

Using the Logging dashboard

You can also view logs for Cloud Functions from the Cloud Console.

Was deze pagina nuttig? Laat ons weten hoe goed we u hebben geholpen:

Feedback verzenden over...

Cloud Functions Documentation