第 2 代 Google Cloud Functions 作为模块部署

问题描述 投票:0回答:1

我正在尝试将第二代 Google Cloud Function(触发事件类型)部署为模块,因为我需要使用 import 语句。

问题是GCP找不到入口点 我应该如何修改以下代码?

index.js

const functions = require('@google-cloud/functions-framework');

// Register a CloudEvent callback with the Functions Framework that will
// be executed when the Pub/Sub trigger topic receives a message.
functions.cloudEvent('printPDFFromHTMLtest2', cloudEvent => {
  // The Pub/Sub message is passed as the CloudEvent's data payload.
  const base64name = cloudEvent.data.message.data;

  const name = base64name
    ? Buffer.from(base64name, 'base64').toString()
    : 'World';

  console.log(`Hello, ${name}!`);
});

package.json

{
  "dependencies": {
    "@google-cloud/functions-framework": "^3.0.0"
  }
}
javascript google-cloud-functions es6-modules
1个回答
0
投票

我已经找到解决办法了。

package.json

{
  "dependencies": {
    "@google-cloud/functions-framework": "^3.0.0"
  },
  "type":"module"
}

index.js

import functions from '@google-cloud/functions-framework';

export const printPDFFromHTMLtest2 = functions.cloudEvent('printPDFFromHTMLtest2', cloudEvent => {
  // The Pub/Sub message is passed as the CloudEvent's data payload.
  const base64name = cloudEvent.data.message.data;

  const name = base64name
    ? Buffer.from(base64name, 'base64').toString()
    : 'World';

  console.log(`Hello, ${name}!`);
});
© www.soinside.com 2019 - 2024. All rights reserved.