Firebase和后端逻辑 [英] Firebase and backend logic

查看:264
本文介绍了Firebase和后端逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是parse.com用户,现在我正在寻找另一个服务。
我怎样才能写回到firebase的结束逻辑?

说让我想验证服务器端的所有值,或触发事情。我想了解一个解决方案,但我想知道推荐的方式。



我认为


  1. 创建使用express的nodejs服务器。
  2. 创建中间件来处理逻辑。
  3. 发送来自应用程序的休息请求,触发中间件
  4. 使用firebase的nodejs sdk根据http请求的参数更新值。

  5. 在应用程序firebase上实现监听变化的处理程序




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

    解决方案

更新(2017年3月10日):虽然下面概述的体系结构仍然有效,可用于将Firebase与任何现有基础结构结合使用,但Firebase刚刚发布了 Firebase云端函数,它允许您在Google服务器上运行JavaScript函数以响应Firebase事件(例如数据库更改,用户签名在这个博客中,Firebase应用程序的通用体系结构已经很好的定义了。

发布 Firebase适合您的应用程序? a>。

您提出的架构最接近架构3,您的客户端代码与两个目录直接将Firebase 添加到您的node.js服务器中。



我强烈建议您考虑选项2,服务器贯穿Firebase。这种类型的体系结构的一个很好的例子是手电筒搜索集成。客户将其搜索查询写入Firebase数据库。服务器监听这样的请求,执行查询并将响应写回数据库。客户端等待这个响应。



这个服务器的一个简单概要可能是:

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

// TODO:执行请求的操作

var responseRef = ref.child('responses')。child(requestSnapshot.key());
responseRef.set(result,function(error){$ b $ if(!error){
//删除请求,因为我们已经处理了
requestSnapshot.ref()。remove();
}
});
})

通过最后一种方法,客户端不会直接与您的服务器进行通信,从而消除所有您必须担心的潜在问题。出于这个原因,我有时把它们称为机器人,而不是服务器。


I am parse.com user, and now I look for another service. How can I write back end logic to 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

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.

解决方案

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).


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

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

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.

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.

这篇关于Firebase和后端逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆