Hosting nodeJS app with firebase

前端 未结 3 867
时光取名叫无心
时光取名叫无心 2020-12-14 21:18

So I have this web-app using angularJS and nodeJS. I don\'t want to just use localhost to demo my project because it doesn\'t looks cool at all when I type \"node server.js\

3条回答
  •  伪装坚强ぢ
    2020-12-14 21:47

    Another idea, is if your nodejs app is independent of the angularjs app (however they use shared data, and perform operations on that data model) you could separate the two and connect them only via firebase.

    Firebase hosting -> index.html and necessary angularjs files.

    Locally (your PC) -> server.js which just connects to firebase and trigger on changed data.

    I have done this for a few projects and it's a handy way to access the outside world (internet) while maintaining some semblence of security by not opening ports blindly.

    I was able to do this to control a chromecast at my house while at a friends house

    Here's an example from my most recent project (I'm trying to make a DVR).

    https://github.com/onaclov2000/webdvr/blob/master/app.js

    var FB_URL = '';
    var Firebase = require('firebase');
    
    var os = require('os')
    var myRootRef = new Firebase(FB_URL);
    
    var interfaces = os.networkInterfaces();
    var addresses = [];
    for (k in interfaces) {
        for (k2 in interfaces[k]) {
            var address = interfaces[k][k2];
            if (address.family == 'IPv4' && !address.internal) {
                addresses.push(address.address)
            }
        }
    }
    
    // Push my IP to firebase
    // Perhaps a common "devices" location would be handy
    var ipRef = myRootRef.push({
        "type": "local",
        "ip": addresses[0]
    });
    
    
    myRootRef.on('child_changed', function(childSnapshot, prevChildName) {
       // code to handle child data changes.
       var data = childSnapshot.val();
       var localref = childSnapshot.ref();
       if (data["commanded"] == "new") {
          console.log("New Schedule Added");
          var schedule = require('node-schedule');
          var date = new Date(data["year"], data["month"], data["day"], data["hh"], data["mm"], 0);
          console.log(date);
          var j = schedule.scheduleJob(date, function(channel, program, length){
                     console.log("Recording Channel " + channel + " and program " + program + " for " + length + "ms");
          }.bind(null, data["channel"], data["program"], data["length"]));
    
          localref.update({"commanded" : "waiting"});
      }
    });
    

    When I change my "commanded" data at the FB_URL, to "new" (which can be accomplished by angularjs VERY Simply, using an ng-click operation for example) it'll schedule a recording for a particular date and time (not all actually functional at the moment).

提交回复
热议问题