[英]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 项目,您将拥有如下目录结构:
您需要做三件事,首先在 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.