Closing a firebase connection in Node.JS

2020-07-02 09:55发布

问题:

I'm trying to create a simple node.JS command-line script to interact with Firebase using their Javascript API. I want the tool to close the Firebase connection and terminate once it has finished it's interaction.

Here is some sample code showing what I am trying to achieve:

var Firebase = require('firebase');
var myRootRef = new Firebase('https://myprojectname.firebaseIO-demo.com/');
myRootRef.set('It's working!');

One possible solution would be adding a callback and calling process.exit():

var Firebase = require('firebase');
var myRootRef = new Firebase('https://myprojectname.firebaseIO-demo.com/');
myRootRef.set("It's working!", function() { 
    process.exit(0); 
});

However, I would love to have a more elegant solution than forcing the process to terminate with process.exit().

Any ideas?

回答1:

i'm not keen on forcing disconnect with process.exit(0) either.. as i only require data persistence (not real-time features) i use the REST API.. I find plain HTTPS twice as fast as the official firebase npm module..

var https = require("https"),
    someData = { some: "data" };

var req = https.request({ 
    hostname: "myprojectname.firebaseio.com", 
    method: "POST", 
    path: "/.json"
});
req.end(JSON.stringify(someData));


回答2:

This can probably be solved with firebase.database().goOffline(). Got this from another thread: https://stackoverflow.com/a/37858899