【问题标题】:Deploying Angular 4 with Node Express Server in Firebase在 Firebase 中使用 Node Express 服务器部署 Angular 4
【发布时间】:2018-03-16 21:50:45
【问题描述】:

我使用 angular cli 创建了一个 angular 4 项目。

现在我安装 express 并且我的 app.js 文件是

app.js

const express = require('express')
const app = express()
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const firebase = require("firebase");

app.use(express.static(path.join(__dirname, 'dist')));

var api = require('./server/routes/api');

app.use('/api', api);
app.get('*', (req, res) => {
     res.render(path.join(__dirname, 'index.html'));

});


// Initialize Firebase
// TODO: Replace with your project's customized code snippet
//NOTE : I have replace the credentials
var config = {
    apiKey: "<API_KEY>",
    authDomain: "<PROJECT_ID>.firebaseapp.com",
   databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
   storageBucket: "<BUCKET>.appspot.com",
  };
firebase.initializeApp(config);

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

现在如果我运行

npm app.js

我的 localhost:3000 正在工作,我的 angular js dist 文件夹显示在浏览器中。

如果我运行 localhost:3000/api 我会收到我的 api 调用。

现在,如何将其部署到 firebase。

我在一个单独的文件夹中尝试了 firebase.init,它正在创建一组 json 文件...

仍然不清楚,如何部署(我需要我的服务器文件夹,dist 文件夹)与 app.js 一起复制到 firebase 应用程序。

希望我很清楚。欢迎有正当理由的反对者。

【问题讨论】:

  • 嗨,Alaksandar:你最终得到这个工作了吗?我也有同样的需求。
  • 对不起。我放弃了,因为对我来说没有任何效果。

标签: node.js angular firebase


【解决方案1】:

我有类似的结构,我所拥有的是:
$ firebase 登录
$ firebase 初始化托管
$ firebase 初始化函数

您必须选择一个 firebase 项目,您将拥有如下目录结构:

    • 功能
      • dist(您构建的 Angular 项目)
      • node_modules
      • index.js(主端点)
      • app.js
      • package.json
    • 公开(删除 index.html)
    • .firebase.c
    • firebase.json

你需要做三件事,首先在firebase.json中放:

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/",
        "function": "your_function_name"
      }
    ]
  }
}

在 index.js 中:

const express = require('express')
const app = require('/app')
const firebase = require("firebase");


// Initialize Firebase
var config = {
    apiKey: "<API_KEY>",
    authDomain: "<PROJECT_ID>.firebaseapp.com",
   databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
   storageBucket: "<BUCKET>.appspot.com",
  };
firebase.initializeApp(config);

exports.your_function_name= functions.https.onRequest(app);

最后是你的 app.js

const express = require('express')
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');

app.use(express.static(path.join(__dirname, 'dist')));

var api = require('./server/routes/api');

app.use('/api', api);

module.exports = app;

要运行这个:
$ firebase serve --only 功能,托管
并部署
$ firebase 部署

【讨论】:

  • .firebase.c 文件中的内容是什么?你提到在public文件夹中应该有index.html文件那么index.html文件中会有什么内容?这个index.html 是指angular build's index.html 文件吗?
  • 是的,public 中的内容是 Angular 的构建。 index.html 和所有其他的 js 东西。我认为 .firebaserc 可以使用 firebase init 的默认配置
猜你喜欢
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 2020-10-07
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多