【问题标题】:Firebase cloud function http request to another firebase cloud functionFirebase 云函数对另一个 Firebase 云函数的 http 请求
【发布时间】:2017-12-24 16:49:28
【问题描述】:

我有一个具有主要功能的主要 Firebase 云功能。但我需要创建另一个更具体的使用主云功能的功能。我可以使用免费帐户从一个 firebase 云功能请求 http 发布或获取到另一个 firebase 云功能吗?

我尝试成功,但收到“ENOTFOUND”消息,我想知道我的代码是否有问题,或者这只是免费帐户的限制。

index.js
'use strict';
const functions = require('firebase-functions');

const express = require('express');
const app = express();

app.post('/test', function(req, res) {
    var request = require('request')

    var options = {
      method: 'post',
      body: req.body, // re-send the incoming body to the main cloud function
      json: true, 
      url: '<main cloud url>',
      headers: req.headers // re-send the incoming headers the main cloud function
    }
    request(options, function (err, response, body) {
      if (err || response.statusCode != 200) {
        res.status(400).send(err); //re-send the received error to the client
        return
      }
      res.send(body); //re-send the received response to the client
    });
});

exports.checkAtivation = functions.https.onRequest(app);

【问题讨论】:

  • 你能分享你的代码吗?
  • 编辑放代码

标签: javascript firebase google-cloud-functions


【解决方案1】:

目前在 Spark 计划中无法从另一个 HTTP 云函数调用一个 HTTP 云函数。您必须升级到 Blaze 计划。

一个项目的函数通过 HTTP 调用其他函数通常是没有意义的。如果您简单地将逻辑抽象为所有端点可以共享的单个函数或模块,则项目中的所有代码都可以在所有函数之间共享。

【讨论】:

  • 这完全正确;尝试在 JavaScript 内部调用您的代码,而不是进行另一个 HTTP 调用。但是,我要补充一点,Blaze 计划的免费配额比 Spark 计划更大,因此您可以升级而无需支付任何费用。
  • @Doug - 有没有关于如何将函数抽象到模块中的教程?
  • 我在从未收到此错误并且仅在部署中更改了一行代码后才收到此错误。 (这不是通过 http 调用 firebase 函数)
猜你喜欢
  • 2017-11-09
  • 2019-03-22
  • 1970-01-01
  • 2020-12-01
  • 2018-04-09
  • 2019-01-18
  • 2021-04-11
  • 2020-06-28
  • 2017-07-31
相关资源
最近更新 更多