14

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" and then go to localhost.....

Since I intend to use Firebase for the data, I have noticed that Firebase provides hosting. I tried it, but it seems to only host the index.html and not through/using server.js. I have customized files for the server to use/update. So, how can I tell Firebase Hosting to use my server and related files when hosting?

Is it possible to tell Firebase, hey, run "node server.js" to host my index.html?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Bones and Teeth
  • 383
  • 1
  • 6
  • 15
  • 6
    Firebase Hosting can only host static files. It will not execute your node.js (or any other) code. But if you current server.js is just serving static files, then you can just upload those to Firebase Hosting and it will work fine. If your server.js does more and you're looking where to host this node.js code, there are a multitude of options a google search away (asking for such recommendations is off-topic for StackOverflow, since there are likely to be as many preferences as there are SO users). – Frank van Puffelen Oct 29 '14 at 18:20
  • 1
    Since you don't want to use localhost/your_project because "it doesn't look cool", consider modifying the hosts file in your computer so as to make any domain (even google.com) point at your localhost. Obviously this trick will work only as long as you are viewing the website from your computer. *winks* – Debojyoti Ghosh Nov 02 '16 at 14:03
  • It's now possible to host node apps - See answer to this "Duplicate" question with more up-to-date details: https://stackoverflow.com/questions/42415677/can-i-use-firebase-hosting-to-write-a-restful-api-in-node-js – Blundering Philosopher Oct 17 '17 at 11:10

3 Answers3

11

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Dan Kanze
  • 18,485
  • 28
  • 81
  • 134
  • Thanks. Yea my last app was purely SPA-ish. That is why I want to get more involved with server stuff. I will try out some nodeJS hosting things. Thanks for the quick response :) – Bones and Teeth Oct 29 '14 at 03:23
  • See answer to this "Duplicate" question with more up-to-date details: https://stackoverflow.com/questions/42415677/can-i-use-firebase-hosting-to-write-a-restful-api-in-node-js – Blundering Philosopher Sep 17 '17 at 08:02
5

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).

onaclov2000
  • 5,741
  • 9
  • 40
  • 54
2

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

Ananda
  • 888
  • 9
  • 19