Google Cloud函数调用托管在Google App Engine上的URL [英] Google Cloud functions call URL hosted on Google App Engine
问题描述
我有一个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屋!