我在访问 Firebase 帐户内的 JSON api 时遇到问题。我将在终端中输入“firebase deploy”,它将返回以下 url,
https://myProjectID.firebaseapp.com https://myProjectID.firebaseapp.com
如果我然后将其输入浏览器,我会得到我的索引.html页面所以我知道它通常有效。但由于我正在尝试访问 API,所以我搜索了 URL/api/app,该 URL/api/app 在我运行“firebaseserve”时有效,但在我运行部署时无效。它返回以下内容
错误:禁止 您的客户端无权从此服务器获取 URL /api/app
但是我的 Gmail 帐户在 Firebase 上是允许的,并且一切都已检查,但 Firebase 和云功能仍然不允许我访问我的 API。请帮忙。
这是我的代码。
INDEX.JS
const functions = require("firebase-functions");
const server = require(__dirname,"../server.js");
const api = functions
.runWith({ memory: "2GB", timeoutSeconds: 120 })
.https.onRequest(server);
module.exports = {
api
};
服务器端JS
const express = require("express");
const path = require("path");
const logger = require("./middleware/logger.js");
const cors = require("cors");
const routes = require("./router/routes.js");
let app = express();
// Init Middleware
app.use(logger);
// Set static folder
app.use(express.static(path.join(__dirname, "public")));
app.use(cors({ origin: true }));
app.use("/", routes);
module.exports = app;
Firebase.JSON
{
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "public",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"function": "server"
}
]
}
}