在本地托管的Firebase应用程序中使用本地模拟的https.onCall Firebase功能

Dav*_*son 7 firebase google-cloud-platform google-cloud-functions

我正在使用客户端的节点SDK和vanilla JavaScript编写Firebase应用程序.我正在使用Firebase Cloud Functions来实现一个服务器,该服务器接收我的页面路由请求并使用该https.onRequest方法返回呈现的HTML .我还使用云功能来处理客户端 - 服务器与https.onCall方法的交互.

我使用该firebase serve命令在本地开发.在本地开发时,我的客户端似乎忽略了我的本地onCall功能,而是调用已部署onCall功能的路由.我被迫部署我的onCall功能,以便在本地查看更改.如果我不部署每个更改,我的本地应用程序将不会显示任何onCall功能更改.出现这种情况我是否运行firebase servefirebase serve --only=hosting,functions.

当我在本地运行我的应用程序时firebase serve,页面通常托管在localhost:5000.这些功能托管在localhost:5001.如果我在其中一个本地托管页面上调用云功能,例如firebase.functions().httpsCallable('functionName')并查看我的开发人员工具中的"网络"面板,我可以看到请求URL https://us-central1-<app-name>.cloudfunctions.net/<functionName>,而不是localhost:5001/<app-name>/us-central1/<functionName>.

这让我感到很沮丧,因为这意味着我必须部署我的函数来测试每个更改,而不是通过我本地托管的Web应用程序测试我的本地函数.

我配置错误了吗?如何让我的本地托管应用程序使用我的本地模拟onCall云功能?

我不是在制作单页应用程序或使用任何视图框架.

Hen*_*ahl 11

看起来Firebase还没有实现一个指向本地服务器的解决方案,所以我提出了一点点破解.在初始化Firebase项目的位置包含以下代码段.希望能帮助到你!

if (process.env.NODE_ENV === 'development') {
  firebase.functions()._url = function (name) {
    return `${process.env.API_URL}/${name}`
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 另外,您可以使用 `functions.useFunctionsEmulator('http://localhost:5001');` https://firebase.google.com/docs/reference/js/firebase.functions.Functions (2认同)

小智 2

Firebase v9.xx 的更新版本:

import { getApp } from "firebase/app";
import { getFunctions, connectFunctionsEmulator, httpsCallable } from "firebase/functions";

const functions = getFunctions(getApp());
connectFunctionsEmulator(functions, "localhost", 5001);

httpsCallable(functions, 'helloWorld')({foo: "bar"}).then((res) => ...)
Run Code Online (Sandbox Code Playgroud)