我正试图使用node和firebase创建后端服务器,但当我试图从中获取数据时,我收到了一个错误:"net::ERR_SSL_PROTOCOL_error"。我的firebase/functions/index.js文件是
const functions = require("firebase-functions");
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("hello firebase");
});
exports.app = functions.https.onRequest(app);
在端口5500上的VSCode liveServer上运行的index.html只是body标签中包含<script>fetch('https://localhost:5000').then(res=>res.text()).then(res=>console.log(res));</script>
的emmet骨架。最后,firebase.json是
{
"functions": {
"predeploy": ["npm --prefix "$RESOURCE_DIR" run lint"]
},
"hosting": {
"rewrites": [
{
"source": "/",
"function": "app"
}
],
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
}
}
我想值得一提的是,当我在浏览器中访问localhost:5000时,我会得到预期的"hello firebase">
@编辑:我已经设置了纯快递服务器,bahaiviour不会更改。
已解决。事实证明,我只需要将cors
添加到服务器中,一切都很好。工作代码为:
const functions = require("firebase-functions");
const express = require("express");
const cors = require("cors");
const app = express();
app.use(cors());
app.get("/", (req, res) => {
res.send("hello firebase");
});
exports.app = functions.https.onRequest(app);