我有一个firebase应用程序,express作为后端,angularjs作为前端。这个应用程序有管理面板和客户端页面,目录结构类似于
App - functions
--admin
---app
---controllers
---views
--client
---app
---controllers
---views
--index.js
-public
--assets
---images
---stylesheets
---javascripts
这是我的express 的index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const serviceAccount = require('./service_account.json');
const app = express();
const firebaseApp = admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://lelangawi.firebaseio.com"
});
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing
app.use(cookieParser());
app.set('views', __dirname);
app.use('/assets', express.static(path.join(__dirname + '../../public/assets')));
app.use('/admin/app', express.static(path.join(__dirname + '../../functions/admin/app')));
app.use('/client/app', express.static(path.join(__dirname + '../../functions/client/app')));
app.use('/admin/controllers', express.static(path.join(__dirname + '../../functions/admin/controllers')));
app.use('/client/controllers', express.static(path.join(__dirname + '../../functions/client/controllers')));
app.engine('html', require('ejs').renderFile);
// CORS Support
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.get('/admin', function (req, res) {
res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
res.render('admin/views/index.html');
});
app.get('/', function (req, res) {
res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
res.render('client/views/index.html');
});
app.get('/admin/partials/:name', function (req, res) {
res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
res.render('admin/views/partials/' + req.params.name);
});
app.get('/client/partials/:name', function (req, res) {
res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
res.render('client/views/partials/' + req.params.name);
});
exports.app = functions.https.onRequest(app);
每次我在控制台中运行firebase-server命令时,都会出现错误:无法获取url。但它可以从应用程序url 中工作
这是托管网址:http://localhost:5000
这是功能url:http://localhost:5001/lelangawi/us-central1/app/
要配置托管路由,必须更改文件firebase.json 上的托管规则
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]}
然后换成这个
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "app"
}
]}