How would I run server-side code in Firebase?

后端 未结 3 1012
长发绾君心
长发绾君心 2020-12-05 05:24

I have a function where I want to perform some server-side validations, but I\"m not sure how to do this? Any suggestions where I should look. THere is nothing in the docu

相关标签:
3条回答
  • 2020-12-05 05:47

    Pattern 2 in this blog article might help. https://firebase.googleblog.com/2013/03/where-does-firebase-fit-in-your-app.html

    In this architecture, Firebase sits between the server and clients. Your servers can connect to Firebase and interact with the data just like any other client would. In other words, your server communicates with clients by manipulating data in Firebase. Our Security and Firebase Rules language lets you assign full access to your data to your server. Your server code can then listen for any changes to data made by clients, and respond appropriately.

    0 讨论(0)
  • 2020-12-05 06:09

    The best way to do this is to create a "pending" node and a "completed" node in Firebase. Every time the client takes an action that requires server validation, have the client add an entry into the pending node. On the server side, you can use the Firebase node.js client (or Java SDK) to listen for changes on the "pending" node, validate the action, and then put it in the "complete" node if the validation succeeds. You'll need to setup your security rules such that only the server code can add items into the "complete" node (for example, by using the secret) - learn more about the Firebase security rules here: https://www.firebase.com/docs/security/security-rules.html

    If your validations are fairly simple to perform, you may be able to do the validation using the security rules themselves - they provide simple string/integer/boolean validation.

    0 讨论(0)
  • 2020-12-05 06:11

    It's late. However, just for someone passing by. Firebase has introduce Cloud Function a month ago. Try to check the official link out. It allow you to put some logics in server side.

    https://firebase.google.com/docs/functions/

    In my understanding instead of thinking about the communication to server as common Request and Response you need to see it as Database trigger event. You can set up the function that will be called when specific action happen.

    0 讨论(0)
提交回复
热议问题