Google Cloud 函数调用托管在 Google App Engine 上的 URL [英] Google Cloud functions call URL hosted on Google App Engine

查看:14
本文介绍了Google Cloud 函数调用托管在 Google App Engine 上的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 firebase 数据库,我希望创建一个云函数,在将子节点添加到父节点时触发,它应该调用一个带有添加在父节点中的子节点参数的 url.

I have a firebase database that I wish to create a cloud function that triggers when adding a child node to the parent node , which should call a url with the parameters of the child node added in the parent node.

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

The URL which would be called is a NodeJS Express app hosted in Google App Engine.

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

How do I do that, if it is even possible?

推荐答案

可以使用node.js request 库来这样做.

You can use the node.js request library to do so.

由于在 Cloud Function 内部,您必须在执行异步任务时返回 Promise,因此您需要使用接口包装器来处理请求,例如 请求-承诺.

Since, inside your Cloud Function, you must return a Promise when performing asynchronous tasks, you will need to use an interface wrapper for request, like request-promise.

您可以按照以下方式做一些事情:

You could do something along these lines:

.....
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) 服务/端点,您可以通过请求正文来完成,例如:


If you want to pass parameters to the HTTP(S) service/endpoint you are calling, you can do it through the body of the request, like:

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

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

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

or through some query string key-value pairs, like:

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

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

这篇关于Google Cloud 函数调用托管在 Google App Engine 上的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆