The Node.js 8 Runtime

The Cloud Functions Node.js 8 runtime is based on Node.js version 8.15.0. For instructions on how to prepare your local machine for Node.js development, see Setting Up a Node.js Development Environment.

To get started with Node.js on Cloud Functions, see the Quickstart.

Selecting the runtime

You can select the Node.js 8 runtime for your function during deployment.

gcloud

If you are using the gcloud command-line tool, you can specify the runtime by using the --runtime parameter. For example:

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

For more arguments that you can specify when you are deploying, see Deploy using the gcloud tool.

Console

If you are using the GCP Console, you can select the runtime when you create and deploy a function.

  1. In the GCP Console, go to the Cloud Functions Overview page.

    Go to the Cloud Functions Overview page

    Make sure that the project for which you enabled Cloud Functions is selected.

  2. Click Create Function.

  3. Under Runtime, select Node.js 8.

Execution environment

The execution environment includes the runtime, the operating system, packages, and a library that invokes your function.

The Node.js 8 runtime uses an execution environment based on Ubuntu 18.04 with Node.js version 8.15.0. See Cloud Functions Execution Environment for more information.

Source code structure

In order for Cloud Functions to find your function's definition, each runtime has certain structuring requirements for your source code. See Writing Cloud Functions for more information.

Specifying dependencies

You can specify dependencies for your functions by listing them in a package.json file. For more information, see Specifying dependencies in Node.js.

Async/await

Node.js 8.x is the first LTS version of Node.js to support the async and await keywords. The following example shows a simple function that uses these new keywords:

const fetch = require('node-fetch');

/**
 * Background Cloud Function demonstrates use of async/await.
 *
 * @param {object} data The event payload.
 */
exports.helloAsync = async data => {
  const result = await fetch('https://www.example.com');
  return result;
};

The function uses the await keyword to wrap an asynchronous HTTP request with a synchronous operation.

Using middleware to handle HTTP requests

Node.js HTTP Cloud Functions provide request and response objects that are compatible with ExpressJS to make consuming HTTP requests simple. Cloud Functions automatically reads the request body, so you will always receive the body of a request independent of the content type. This means that HTTP requests should be considered to have been fully read by the time your code is executed. The nesting of ExpressJS apps should be used with this caveat—specifically, middleware that expects the body of a request to be unread might not behave as expected.

Cette page vous a-t-elle été utile ? Évaluez-la :

Envoyer des commentaires concernant…

Cloud Functions Documentation