当前位置 : 主页 > 网页制作 > Nodejs >

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

来源:互联网 收集:自由互联 发布时间:2021-06-16
我有一个firebase数据库,我希望创建一个云功能,在将父节点添加到父节点时触发,该父节点应调用带有父节点中添加的子节点参数的url. 将被调用的URL是在Google App Engine中托管的NodeJS Expre
我有一个firebase数据库,我希望创建一个云功能,在将父节点添加到父节点时触发,该父节点应调用带有父节点中添加的子节点参数的url.

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

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

您可以使用node.js request库来执行此操作.

因为,在您的Cloud Function中,您必须在执行异步任务时返回Promise,您需要使用接口包装器来处理请求,例如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
      };
      .....
网友评论