【问题标题】:Firebase-node.js localhost server throwing SSL_PROTOCOL error when i try to fetch data from itFirebase-node.js localhost 服务器在我尝试从中获取数据时抛出 SSL_PROTOCOL 错误
【发布时间】:2020-08-18 07:17:36
【问题描述】:

我正在尝试使用 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);


在 VSCode liveServer 的 5500 端口上运行的 index.html 只是主体标签中带有 <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”

@edit:我设置了纯快递服务器,bahaiviour 没有变化。

【问题讨论】:

    标签: javascript node.js firebase google-cloud-functions firebase-hosting


    【解决方案1】:

    解决了。结果我只需将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);
    

    【讨论】:

      猜你喜欢
      • 2020-08-10
      • 2023-01-09
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 2020-05-14
      相关资源
      最近更新 更多