node.js Firebase 和后端逻辑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35360421/
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-09-02 19:54:10  来源:igfitidea点击:

Firebase and backend logic

node.jsfirebase

提问by Alon

I am parse.com user, and now I look for another service. How can I write back end logic to firebase?

我是 parse.com 用户,现在我在寻找其他服务。如何将后端逻辑写入 firebase?

let say I want to validate all the values on server side, or trigger things. I thought about one solution, but I want to know the recommended way.

假设我想验证服务器端的所有值,或者触发一些事情。我想到了一种解决方案,但我想知道推荐的方法。

I think to

我认为

  1. create nodejs server, that uses express.
  2. create middlewares to handle the logic.
  3. send rest request from the app, that triggers the middlewares
  4. use the nodejs sdk of firebase to update the values according to the params of the http request.
  5. And implement on the app firebase handler that listen to changes
  1. 创建使用 express 的 nodejs 服务器。
  2. 创建中间件来处理逻辑。
  3. 从应用程序发送休息请求,触发中间件
  4. 使用firebase的nodejs sdk根据http请求的参数更新值。
  5. 并在监听更改的应用程序 firebase 处理程序上实现

enter image description here

在此处输入图片说明

their something simpler? In parse I used cloud code, I want that the logic will not be on the client side but on a server side.

他们的东西更简单?在解析中,我使用了云代码,我希望逻辑不在客户端,而是在服务器端。

回答by Frank van Puffelen

Update (March 10, 2017): While the architecture I outline below is still valid and can be used to combine Firebase with any existing infrastructure, Firebase just released Cloud Functions for Firebase, which allows you to run JavaScript functions on Google's servers in response to Firebase events (such as database changes, users signing in and much more).

更新(2017 年 3 月 10 日)虽然我在下面概述的架构仍然有效并且可用于将 Firebase 与任何现有基础架构相结合,但 Firebase 刚刚发布了Cloud Functions for Firebase,它允许您在 Google 的服务器上运行 JavaScript 函数以响应Firebase 事件(例如数据库更改、用户登录等)。



The common architectures of Firebase applications are pretty well-defined in this blog post Where does Firebase fit in your app?.

Firebase 应用程序的常见架构在这篇博文中得到了很好的定义Firebase 在您的应用程序中的位置?.

The architecture you propose is closest to architecture 3, where your client-side code talks both directly to Firebase andto your node.js server directly.

您提出的架构与架构 3 最接近,其中您的客户端代码直接与 Firebasenode.js 服务器对话。

I also highly recommend that you consider option 2, where all interaction between clients and server runs through Firebase. A great example of this type of architecture is the Flashlight search integration. Clients write their search queries into the Firebase database. The server listens for such requests, executes the query and writes the response back to the database. The client waits for that response.

我还强烈建议您考虑选项 2,其中客户端和服务器之间的所有交互都通过 Firebase 运行。这种架构的一个很好的例子是Flashlight 搜索集成。客户将他们的搜索查询写入 Firebase 数据库。服务器侦听此类请求,执行查询并将响应写回数据库。客户端等待该响应。

A simple outline for this server could be:

此服务器的简单概述可能是:

var ref = new Firebase('https://yours.firebaseio.com/searches');
ref.child('requests').on('child_added', function(requestSnapshot) {

    // TODO: execute your operation for the request

    var responseRef = ref.child('responses').child(requestSnapshot.key());
    responseRef.set(result, function(error) {
        if (!error) {
            // remove the request, since we've handled it
            requestSnapshot.ref().remove();
        }
    });
})

With this last approach the client never directly talks to your server, which removes all kind of potential problems that you have to worry about. For this reason I sometimes refer to them as "bots", instead of servers.

使用最后一种方法,客户端永远不会直接与您的服务器对话,这消除了您必须担心的所有潜在问题。出于这个原因,我有时将它们称为“机器人”,而不是服务器。

回答by Alon

2017

2017年

Today Google announced Cloud Functions for Firebasehttps://firebase.google.com/features/functions/

今天谷歌宣布了 Firebase 的云函数https://firebase.google.com/features/functions/

This is a great solution for the architectures and back end logic in Firebase.

这是 Firebase 架构和后端逻辑的绝佳解决方案。

回答by ThadeuLuz

Here's what I would do:

这是我会做的:

  • Validade all the inputs with the ".validate" rules. No server needed for that.
  • If you have tasks to run, use Firebase Queue, a bot to run the tasks and you are done.
  • 使用“.validate ”规则验证所有输入。不需要服务器。
  • 如果您有任务要运行,请使用Firebase Queue,一个机器人来运行任务,您就完成了。

If you don't do the last one, you may have two problems:

如果你不做最后一个,你可能会遇到两个问题:

  • If you try use the diagram you posted it will be a little tricky to get the auth object at the server (but not impossible). Go ahead if you don't need to validate the user to allow the request.

  • If you use just the regular firebase app to listen to changes and respond (editing the object for instance, like Frank van Puffelen's example code), you might have scalability problems. Once your back end scales to two (or more) instances, a firebase edit will trigger the task on all of them. Each instance will notice there was a change, then run the same task once each, add/replace the response object once each and try to remove the request object once each..

  • 如果您尝试使用您发布的图表,在服务器上获取 auth 对象会有点棘手(但并非不可能)。如果您不需要验证用户以允许请求,请继续。

  • 如果您只使用常规的 firebase 应用程序来监听更改和响应(例如编辑对象,如 Frank van Puffelen 的示例代码),您可能会遇到可扩展性问题。一旦您的后端扩展到两个(或更多)实例,firebase 编辑将在所有实例上触发任务。每个实例都会注意到有一个变化,然后每个运行一次相同的任务,每个添加/替换响应对象并尝试删除请求对象一次。

Using Firebase Queue avoids both of these problems.

使用 Firebase Queue 可以避免这两个问题。