admin管理员组

文章数量:1414908

I want to use firebase as a database service to my server to which a android app would send requests to. I want the server to retrieve data rather than the android app because I want to do some processing before sending data back to client (app).

My node code (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",
    databaseURL: "",
    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)

I cannot use <script src=".12.1/firebase.js"></script> cause I'm not connecting to firebase from client. So I instead copied the code from .12.1/firebase.js and imported it into my index.js using require() . When I run the index.js I get:

firebase.initializeApp(config);
         ^

TypeError: firebase.initializeApp is not a function
    at Object.<anonymous> (E:\workspace_javascript\firebase_try\index.js:24:10)
    at Module._pile (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

It works well when I run it by returning a html with firebase code inside <script>

I want to use firebase as a database service to my server to which a android app would send requests to. I want the server to retrieve data rather than the android app because I want to do some processing before sending data back to client (app).

My node code (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.",
    databaseURL: "https://hnotgonnatell.firebaseio.",
    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)

I cannot use <script src="https://www.gstatic./firebasejs/4.12.1/firebase.js"></script> cause I'm not connecting to firebase from client. So I instead copied the code from https://www.gstatic./firebasejs/4.12.1/firebase.js and imported it into my index.js using require() . When I run the index.js I get:

firebase.initializeApp(config);
         ^

TypeError: firebase.initializeApp is not a function
    at Object.<anonymous> (E:\workspace_javascript\firebase_try\index.js:24:10)
    at Module._pile (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

It works well when I run it by returning a html with firebase code inside <script>

Share Improve this question asked Apr 13, 2018 at 15:56 ParmuTownleyParmuTownley 1,0072 gold badges19 silver badges39 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5
  1. Install firebase from NPM

      npm install --save firebase-admin
    
  2. Initiallize 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.'
      });
    

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

If you want to access Firebase products on a server, you should be using the Firebase Admin SDK, not the client SDK.

本文标签: javascriptHow to run firebase on server sideStack Overflow