仅在Node上就已经回答了所有问题,但是当它是Firebase上托管的Express服务器时,似乎找不到答案。
我遇到的问题是无论我做什么都无法检测到config.css文件。 我曾尝试使用快速中间件来更改根目录,但存在一些问题,其中指出MIME类型不正确。其他时间则显示404未找到。
让我知道是否需要提供其他详细信息。
index.js文件:
const functions = require('firebase-functions');
const firebase = require('firebase-admin');
const express = require('express');
const engines = require('consolidate');
const bodyParser = require("body-parser");
var config = {
<config details here>
};
const firebaseApp = firebase.initializeApp(functions.config().firebase);
const database = firebase.database();
const hbs = require('handlebars');
const getUpdates = () => {
return new Promise((resolve, reject) => {
try {
const ref = firebaseApp.database().ref('servers');
resolve(ref.orderByChild("sortDate").once("value"), ref);
} catch (e) {
reject(e);
}
});
};
const app = express();
app.engine('hbs', engines.handlebars);
app.set('views', './views');
app.set('view engine', 'hbs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post("/config", function (req, res) {
let pushObject = req.body;
pushObject.time = new Date();
pushObject.sortDate = new Date();
console.log(pushObject)
firebase.database().ref('servers/testserver').set(pushObject, (error) => {
if (error) {
console.log(error);
} else {
console.log('Message pushed')
}
});
res.redirect('back');
});
app.get('/config', (request, response) => {
response.set('Cache-Control', 'public, max-age=0, s-maxage=0');
getUpdates().then((updates, ref) => {
const updatesList = updates.val();
response.render('index', updatesList );
return;
});
});
exports.app = functions.https.onRequest(app);
index.hbs文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" type="text/css" href="/config.css">
<title>Config Server</title>
</head>
<body>
<div class="navbar">
{{test}}
</div>
</body>
</html>
</html>