使用 Node.js 创建和部署 HTTP Cloud Functions 函数

使用 Node.js 创建和部署 HTTP Cloud Functions 函数

本文档将引导您完成创建简单的 Cloud Functions HTTP 函数的过程。这是两种 Cloud Functions 函数之一:

  • HTTP 函数,通过标准 HTTP 请求调用。
  • 事件驱动型函数,由云基础设施中的事件触发,例如 Pub/Sub 主题中收到消息或 Cloud Storage 存储桶发生更改。

如需了解详情,请参阅编写 HTTP 函数编写事件驱动型函数

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 Cloud Functions、Cloud Build、Artifact Registry、Cloud Run 和 Cloud Logging API。

    启用 API

  5. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  6. 确保您的 Google Cloud 项目已启用结算功能

  7. 启用 Cloud Functions、Cloud Build、Artifact Registry、Cloud Run 和 Cloud Logging API。

    启用 API

  8. 安装并初始化 gcloud CLI
  9. 更新并安装 gcloud 组件。
    gcloud components update
  10. 准备开发环境。

    转到 Node.js 设置指南

创建函数

  1. 在本地系统上为函数代码创建一个目录:

    Linux 或 Mac OS X

    mkdir ~/helloworld
    cd ~/helloworld
    

    Windows

    mkdir %HOMEPATH%\helloworld
    cd %HOMEPATH%\helloworld
    
  2. helloworld 目录中创建一个 index.js 文件,其中包含以下内容:

    const functions = require('@google-cloud/functions-framework');
    
    // Register an HTTP function with the Functions Framework that will be executed
    // when you make an HTTP request to the deployed function's endpoint.
    functions.http('helloGET', (req, res) => {
      res.send('Hello World!');
    });

    此示例函数会向所有请求返回令人喜悦的“Hello World!”。

指定依赖项

Node.js 依赖项可通过 npm 进行管理,并在名为 package.json 的元数据文件中表示。 您可以手动创建此文件,也可以使用 npm 命令创建。

  • 要使用 npm 命令创建 package.json 文件,请执行以下操作:

    1. 从 helloworld 目录运行 npm init 命令。按 Enter 可接受问题的默认答案。

      npm init
      
    2. 修改 package.json 文件以添加函数框架依赖项:

      "dependencies": {
        "@google-cloud/functions-framework": "^3.1.0"
      }
      
  • 如果您希望手动创建 package.json 文件,请将以下内容复制到其中:

{
  "name": "nodejs-docs-samples-functions-hello-world-get",
  "version": "0.0.1",
  "private": true,
  "license": "Apache-2.0",
  "author": "Google Inc.",
  "repository": {
    "type": "git",
    "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
  },
  "engines": {
    "node": ">=16.0.0"
  },
  "scripts": {
    "test": "c8 mocha -p -j 2 test/*.test.js --timeout=6000 --exit"
  },
  "dependencies": {
    "@google-cloud/functions-framework": "^3.1.0"
  },
  "devDependencies": {
    "c8": "^8.0.0",
    "gaxios": "^6.0.0",
    "mocha": "^10.0.0",
    "wait-port": "^1.0.4"
  }
}

许多 Node.js 客户端库都可用于 Google Cloud 产品,并且可以作为依赖项安装。

在本地构建和测试函数

如需在部署函数之前在本地对其进行测试,您必须在本地安装函数框架,然后运行函数。

  1. helloworld 目录运行以下命令,以在本地机器上安装函数框架:

    npm install @google-cloud/functions-framework
    
  2. helloworld 目录运行以下命令,以在本地运行您的函数:

    npx @google-cloud/functions-framework --target=helloGET
    
  3. 在浏览器中访问 http://localhost:8080,或从其他窗口运行 curl localhost:8080,以测试函数。

    如需了解详情,请参阅向本地函数发送请求

该函数返回“Hello World!”消息

部署函数

如需部署函数,请在 helloworld 目录中运行 gcloud functions deploy 命令:

gcloud functions deploy hello-node-function \
  --gen2 \
  --runtime=nodejs20 \
  --region=REGION \
  --source=. \
  --entry-point=helloGET \
  --trigger-http \
  --allow-unauthenticated

REGION 替换为要部署函数的 Google Cloud 区域的名称(例如 us-west1)。

这会在您选择的区域中使用 nodejs20 运行时部署您的示例函数。

通过可选的 --allow-unauthenticated 标志,您可以在不进行身份验证的情况下访问函数。

测试已部署的函数

  1. 函数部署后,请记下 gcloud functions deploy 命令输出中的 uri 属性,或使用以下命令检索该属性:

      gcloud functions describe hello-node-function \
        --region=REGION
    

    REGION 替换为您在其中部署函数的 Google Cloud 区域的名称(例如 us-west1)。

  2. 在浏览器中或通过以下 curl 命令访问此网址:

    curl FUNCTION_URL
    

    FUNCTION_URL 替换为您刚刚检索到的 uri 属性。

    该函数将返回一条“Hello World!”消息。

查看函数日志

使用命令行工具查看日志

您可以使用 Cloud Logging 界面或通过 Google Cloud CLI 查看函数的日志。

如需使用 gcloud CLI 查看函数的日志,请使用 gcloud functions logs read 命令:

    gcloud functions logs read \
      --gen2 \
      --region=REGION \
      --limit=10 \
      hello-node-function

REGION 替换为您在其中部署函数的 Google Cloud 区域的名称(例如 us-west1)。

输出类似以下内容:

LEVEL: I
NAME: hello-node-function
TIME_UTC: 2023-06-16 18:42:24.956
LOG:

LEVEL: I
NAME: hello-node-function
TIME_UTC: 2023-06-16 18:42:01.692
LOG:

LEVEL: I
NAME: hello-node-function
TIME_UTC: 2023-06-16 18:31:47.711
LOG: Default STARTUP TCP probe succeeded after 1 attempt for container "hello--node--function-1" on port 8080.

LEVEL: I
NAME: hello-node-function
TIME_UTC: 2023-06-16 18:31:46.542
LOG:

LEVEL: I
NAME: hello-node-function
TIME_UTC: 2023-06-16 18:31:27.390
LOG: Default STARTUP TCP probe succeeded after 1 attempt for container "hello--node--function-1" on port 8080.

使用日志记录信息中心查看日志

要使用日志记录信息中心查看函数的日志,请打开 Cloud Functions 概览页面,点击列表中的函数名称,然后点击日志标签页。