繁体   English   中英

如何在服务器端运行Firebase?

[英]How to run firebase on server side?

我想将firebase用作我的Android应用程序向其发送请求的服务器的数据库服务。 我希望服务器检索数据而不是Android应用程序,因为我想在将数据发送回客户端(应用程序)之前进行一些处理。

我的节点代码(index.js):

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

var port = process.env.PORT || 10000

firebase = require('./firebase') // I added this cuz I can't use <script> tag here

// Initialize Firebase
var config = {
    apiKey: "AIzaSynotgonnatell2Q-kwk85XrCyNnc",
    authDomain: "hnotgonnatell.firebaseapp.com",
    databaseURL: "https://hnotgonnatell.firebaseio.com",
    projectId: "notgonnatell",
    storageBucket: "",
    messagingSenderId: "699935506077"
};

firebase.initializeApp(config);

var database = firebase.database()

ref = database.ref("some_table")

data = {
    name : "my name",
    number : "2938019283"
}

app.get('/', function(req, res){
    ref.push(data)
    res.send("hello")
})

app.listen(port)

我无法使用<script src="https://www.gstatic.com/firebasejs/4.12.1/firebase.js"></script>因为我没有从客户端连接到Firebase。 所以我改为从https://www.gstatic.com/firebasejs/4.12.1/firebase.js复制代码,然后使用require()将其导入到我的index.js中。 当我运行index.js时,我得到:

firebase.initializeApp(config);
         ^

TypeError: firebase.initializeApp is not a function
    at Object.<anonymous> (E:\workspace_javascript\firebase_try\index.js:24:10)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:422:7)
    at startup (bootstrap_node.js:143:9)
    at bootstrap_node.js:537:3

当我通过在<script>内返回带有firebase代码的html来运行它时效果很好

  1. 从NPM安装Firebase

      npm install --save firebase-admin 
  2. 初始化Firebase

      var admin = require('firebase-admin'); var serviceAccount = require('path/to/serviceAccountKey.json'); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: 'https://<DATABASE_NAME>.firebaseio.com' }); 
  3. ???

  4. 利润

src: https//firebase.google.com/docs/admin/setup

如果要访问服务器上的Firebase产品,则应使用Firebase Admin SDK而不是客户端SDK。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM