Hosting nodeJS app with firebase

前端 未结 3 864
时光取名叫无心
时光取名叫无心 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:30

    I might be late but since 3 years have passed there is an solution available now from Firebase in the form of cloud functions Its not straight forward but looks promising if one can refactor their code a bit

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-12-14 21:50

    I'm guessing by the way you are wording the question you want to see this site from "the internet".

    Two routes you could go here.

    a) Serve your index through Firebase hosting. Firebase only hosts assets. If your Angular app is being served through Node then you will need to change your architecture to be more SPA-ish

    SPA-ish would be like an index bootstrap that interacts with the backend purely through API's.

    You would host the API server on something more appropriate like through Nodejitsu.

    b) Serve the whole thing through something like Nodejitsu (hosting platform) or your very own VM managed by a different kind of hosting company like BuyVM.net.

    0 讨论(0)
提交回复
热议问题