Javascript 使用 firebase 云功能向非谷歌服务器发送 POST 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43415759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:54:44  来源:igfitidea点击:

Use firebase cloud function to send POST request to non-google server

javascriptfirebasegoogle-cloud-functions

提问by Stone Preston

I was wondering if its possible to use a firebase cloud function to send a post request to a non-google server (from what I can find I need to be on the blaze plan in order to interact with non google servers)

我想知道是否可以使用 firebase 云功能向非谷歌服务器发送发布请求(据我所知,我需要参与 blaze 计划才能与非谷歌服务器进行交互)

Basically I want to POST to an external server running on an arduino whenever a value is added to my database.

基本上,每当将值添加到我的数据库时,我都想 POST 到在 arduino 上运行的外部服务器。

I have looked through the docs and found examples of having a cloud function respond to an HTTP post request (HTTP cloud functions) but can't seem to find any examples of posting to an external server. Is this possible?

我浏览了文档并找到了让云功能响应 HTTP 发布请求(HTTP 云功能)的示例,但似乎找不到任何发布到外部服务器的示例。这可能吗?

回答by stodi

This can be done using the requestmodule:

这可以使用request模块来完成:

// import the module
var request = require('request');

// make the request
request('put your external url here', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        //here put what you want to do with the request
    }
})

NOTE: This will only work on paid plans. It is not possible to call non-google APIs using the free Spark plan as explained on the Firebase pricing page:

注意:这仅适用于付费计划。如Firebase 定价页面所述,无法使用免费 Spark 计划调用非谷歌 API :

The Spark plan allows outbound network requests only to Google-owned services. Inbound invocation requests are allowed within the quota. On the Blaze plan, Cloud Functions provides a perpetual free tier. The first 2,000,000 invocations, 400,000 GB-sec, 200,000 CPU-sec, and 5 GB of Internet egress traffic is provided for free each month. You are only charged on usage past this free allotment. Pricing is based on total number of invocations, and compute time. Compute time is variable based on the amount of memory and CPU provisioned for a function. Usage limits are also enforced through daily and 100s quotas. For more information, see Cloud Functions Pricing.

Spark 计划只允许对 Google 拥有的服务发出出站网络请求。配额内允许入站调用请求。在 Blaze 计划中,Cloud Functions 提供永久免费层。每月免费提供前 2,000,000 次调用、400,000 GB 秒、200,000 CPU 秒和 5 GB 的 Internet 出口流量。您只需为超过此免费配额的使用量付费。定价基于调用总数和计算时间。计算时间根据为函数配置的内存和 CPU 量而变化。使用限制也通过每日和 100 秒配额强制执行。有关更多信息,请参阅Cloud Functions 定价

回答by RodolfoNeto

You need to install the package. Go to Firebase-Funcions directory in Terminal and type

您需要安装该软件包。转到终端中的 Firebase-Funcions 目录并输入

npm install request

OR

或者

npm install request-promise

Use this example for test: https://www.npmjs.com/package/request

使用此示例进行测试:https: //www.npmjs.com/package/request

回答by Cesare

Remember to install the module within the functions folder!

请记住将模块安装在函数文件夹中!

cd functions
npm i --save request

回答by Ruan

For those of you who want to post with a JSON body this is how you can do it. (I know I needed this a while ago)

对于那些想要使用 JSON 正文发帖的人,您可以这样做。(我知道我不久前需要这个)

export function postWithBodyToExternalUrl(url: string, bdy: any): Promise<ReqResponse> {

  const request = require('request');

  const options = {
    url: url,
    json: true
  };
  return new Promise(function (resolve, reject) {
    request(options, function (err, resp) {
      if (err) {
        console.log(err);
        reject({ err: err });
      }
      resolve(bdy);
    });
  });
}

回答by Hemant Kaushik

axiosis also one of the great library to handle network calls. Some features:

axios也是处理网络调用的优秀库之一。一些特点:

  • Make XMLHttpRequests from the browser
  • Make http requests from node.js
  • Supports the Promise API
  • Intercept request and response
  • Transform request and response data
  • Cancel requests
  • Automatic transforms for JSON data
  • Client side support for protecting against XSRF
  • 67k+stars on github
  • github documentation for more
  • 从浏览器发出 XMLHttpRequests
  • 从 node.js 发出 http 请求
  • 支持 Promise API
  • 拦截请求和响应
  • 转换请求和响应数据
  • 取消请求
  • JSON 数据的自动转换
  • 客户端支持防止 XSRF
  • github 上67k+颗星
  • github 文档了解更多