简体   繁体   English

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

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

I created an angular 4 project using angular cli.我使用 angular cli 创建了一个 angular 4 项目。

Now i install express and my app.js file is现在我安装 express 并且我的 app.js 文件是

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!')
})

Now if i run现在如果我跑

npm app.js

my localhost:3000 is working and my angular js dist folder is displayed in the browser.我的 localhost:3000 正在工作,我的 angular js dist 文件夹显示在浏览器中。

If i run localhost:3000/api i am getting my api calls.如果我运行 localhost:3000/api 我会收到我的 api 调用。

Now, how to deploy this to firebase.现在,如何将其部署到 firebase。

I tried firebase.init in a seperate folder, which was creating a set of json files...我在一个单独的文件夹中尝试了 firebase.init ,该文件夹正在创建一组 json 文件......

Still its not clear, how to deploy (i need my server folder, dist folder )to be copied along with app.js to firebase app.仍然不清楚,如何部署(我需要我的服务器文件夹,dist 文件夹)与 app.js 一起复制到 firebase 应用程序。

Hope i was clear.希望我很清楚。 Downvoters are welcome with proper reasoning.有适当推理的反对者是受欢迎的。

I have similar structure and what i have is:我有类似的结构,我有的是:
$ firebase login $ firebase登录
$ firebase init hosting $ firebase 初始化托管
$ firebase init functions $ firebase 初始化函数

You have to choose a firebase project, and you will have a directory structure like this:您必须选择一个 firebase 项目,您将拥有如下目录结构:

  • root
    • functions职能
      • dist (your built angular project) dist(您构建的角度项目)
      • node_modules节点模块
      • index.js (main endpoint) index.js(主要端点)
      • app.js应用程序.js
      • package.json包.json
    • public (delete index.html)公开(删除 index.html)
    • .firebase.c .firebase.c
    • firebase.json firebase.json

There are three things you need to do, first in firebase.json put:您需要做三件事,首先在 firebase.json 中放入:

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

In index.js:在 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);

And finally your app.js最后你的 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;

To run this:要运行这个:
$ firebase serve --only functions,hosting $ firebase serve --only 功能,托管
and to deploy并部署
$ firebase deploy $ firebase 部署

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM