簡體   English   中英

在 Firebase 中使用 Node Express 服務器部署 Angular 4

[英]Deploying Angular 4 with Node Express Server in Firebase

我使用 angular cli 創建了一個 angular 4 項目。

現在我安裝 express 並且我的 app.js 文件是

應用程序.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 應用程序。

希望我很清楚。 有適當推理的反對者是受歡迎的。

我有類似的結構,我有的是:
$ firebase登錄
$ firebase 初始化托管
$ firebase 初始化函數

您必須選擇一個 firebase 項目,您將擁有如下目錄結構:

    • 職能
      • dist(您構建的角度項目)
      • 節點模塊
      • index.js(主要端點)
      • 應用程序.js
      • 包.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 部署

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM