14

所以我有这个使用 angularJS 和 nodeJS 的网络应用程序。我不想只使用 localhost 来演示我的项目,因为当我键入“node server.js”然后转到 localhost 时,它看起来一点也不酷.....

由于我打算将 Firebase 用于数据,因此我注意到 Firebase 提供托管。我试过了,但它似乎只托管 index.html 而不是通过/使用 server.js。我已经为服务器定制了文件来使用/更新。那么,如何让 Firebase Hosting 在托管时使用我的服务器和相关文件?

是否可以告诉 Firebase,嘿,运行“node server.js”来托管我的 index.html?

4

3 回答 3

10

我猜你想从“互联网”看到这个网站的问题的措辞。

你可以去这里的两条路线。

a) 通过 Firebase 托管服务您的索引。Firebase 仅托管资产。如果您的 Angular 应用程序是通过 Node 提供服务的,那么您将需要更改您的架构以更符合SPA风格

SPA-ish 就像一个索引引导程序,纯粹通过 API 与后端交互。

您可以将 API 服务器托管在更合适的地方,例如通过 Nodejitsu。

b) 通过 Nodejitsu(托管平台)或您自己的 VM 提供整个服务,这些 VM 由不同类型的托管公司(如 BuyVM.net)管理。

于 2014-10-29T03:17:44.087 回答
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).

于 2015-01-06T07:01:10.097 回答
2

我可能迟到了,但自从 3 年过去了,Firebase 现在以云函数的形式提供了一个解决方案 它不是直截了当的,但如果有人可以稍微重构他们的代码,看起来很有希望

于 2017-06-19T14:02:31.240 回答