从 App Engine 应用创建任务

用于教程的 App Engine 应用:使用 Cloud Tasks 触发 Cloud Functions 函数。

包含此代码示例的文档页面

代码示例

Node.js

如需了解如何安装和使用 Cloud Tasks 客户端库,请参阅 Cloud Tasks 客户端库

app.post('/send-email', (req, res) => {
  // Set the task payload to the form submission.
  const {to_name, from_name, to_email, date} = req.body;
  const payload = {to_name, from_name, to_email};

  createHttpTaskWithToken(
    process.env.GOOGLE_CLOUD_PROJECT,
    QUEUE_NAME,
    QUEUE_LOCATION,
    FUNCTION_URL,
    SERVICE_ACCOUNT_EMAIL,
    payload,
    date
  );

  res.status(202).send('📫 Your postcard is in the mail! 💌');
});