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.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)
I cannot use <script src="https://www.gstatic.com/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.com/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._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
It works well when I run it by returning a html with firebase code inside <script>
Install firebase from NPM
npm install --save firebase-admin
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.com'
});
src: https://firebase.google.com/docs/admin/setup
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With