node.js - 托管在 Google App Engine 上的 Google Cloud 函数调用 URL

标签 node.js firebase google-cloud-functions

我有一个 firebase 数据库,我希望创建一个云函数,该函数在将子 Node 添加到父 Node 时触发,它应该调用一个 url,其中包含在父 Node 中添加的子 Node 的参数。

将调用的 URL 是托管在 Google App Engine 中的 NodeJS Express 应用。

如果可能的话,我该怎么做?

最佳答案

您可以使用 node.js request图书馆这样做。

由于在您的 Cloud Function 中,您必须在执行异步任务时返回一个 Promise,因此您需要为请求使用接口(interface)包装器,例如 request-promise .

你可以按照这些思路做一些事情:

.....
var rp = require('request-promise');
.....

exports.yourCloudFucntion = functions.database.ref('/parent/{childId}')
    .onCreate((snapshot, context) => {
      // Grab the current value of what was written to the Realtime Database.
      const createdData = snapshot.val();

      var options = {
          url: 'https://.......',
          method: 'POST',
          body: ....
          json: true // Automatically stringifies the body to JSON
      };

      return rp(options);

    });

如果您想将参数传递给您正在调用的 HTTP(S) 服务/端点,您可以通过请求的主体来完成,例如:

      .....
      const createdData = snapshot.val();

      var options = {
          url: 'https://.......',
          method: 'POST',
          body: {
              some: createdData.someFieldName
          },
          json: true // Automatically stringifies the body to JSON
      };
      .....

或者通过一些查询字符串键值对,比如:

      .....
      const createdData = snapshot.val();
      const queryStringObject = { 
         some: createdData.someFieldName,
         another: createdData.anotherFieldName
      };

      var options = {
          url: 'https://.......',
          method: 'POST',
          qs: queryStringObject
      };
      .....

关于node.js - 托管在 Google App Engine 上的 Google Cloud 函数调用 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52526521/

相关文章:

javascript - 如何访问匿名 JSON 属性?

node.js - 运行 pm2 日志错误 SyntaxError : Unexpected token

FirebaseAuth 禁用用户检查并注销?

android - Firebase 实时数据库现在无法正常工作?

javascript - "Converting circular structure to JSON"BigQuery 从云函数 NodeJs 插入

node.js - zlib:zlib 绑定(bind)​​已关闭

node.js - 查找、修改和删除递归嵌入文档 mongoose js

swift - 如何从快照闭包返回数组的值并使用它来准备segue?

javascript - 如何在云函数中制作一个可以在其他函数中调用的函数?

python - 使用 Cloud Functions 在 Google Cloud Platform 中设置环境变量