[英]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來運行它時效果很好
從NPM安裝Firebase
npm install --save firebase-admin
初始化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' });
???
如果要訪問服務器上的Firebase產品,則應使用Firebase Admin SDK而不是客戶端SDK。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.