Firebase-node.js localhost服务器在我试图从它那里获取数据时抛出SSL_PROTOCOL错误。

问题描述 投票:0回答:1

我试图使用node和firebase创建我的后台服务器,但是当我试图从它那里获取数据时,我得到了一个错误的信息:"net::ERR_SSL_PROTOCOL_ERROR"。"net::ERR_SSL_PROTOCOL_ERROR"。我的firebasefunctionsindex.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,则是以 <script>fetch('https://localhost:5000').then(res=>res.text()).then(res=>console.log(res));</script> 中的body标签。最后,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没有变化。

javascript node.js firebase google-cloud-functions firebase-hosting
1个回答
0
投票

解决了。原来,我只需要在文件中添加 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);
© www.soinside.com 2019 - 2024. All rights reserved.