繁体   English   中英

POST https://localhost:5000/stored net::ERR_SSL_PROTOCOL_ERROR

[英]POST https://localhost:5000/stored net::ERR_SSL_PROTOCOL_ERROR

我试图调用 API 来获取数据并将其从我的反应应用程序移动到 mongodb 到节点 js 服务器,但我不断收到该错误以及控制台中的另一个错误。

在此处输入图像描述

我试图尽可能多地使用链接,但似乎没有任何效果。

这是我在 pingbutton.js 文件中获取的函数

handleSubmit = () => {
    console.log("its running");
    let databody = {
      message: this.state.val,
    };
    console.log(" the message is :" + this.state.val);
    return fetch("https://localhost:5000/stored", {
      method: "POST",
      body: JSON.stringify(databody),
      headers: {
        "Content-Type": "application/json",
      },
    })
      .then((res) => res.json())
      .then((data) => console.log(data));
  };

这是我在服务器端的 index.js:

const express = require("express");
const cors = require("cors"); // Importing cors
var request = require("request");
const dotenv = require("dotenv");
const port = 5000;
var request = require("request");
var util = require("util");
const connectDB = require("./config/db");
require("dotenv").config({ path: "./config/config.env" });


const app = express();
dotenv.config();
connectDB();

app.get("/", (req, res) => {
  res.send("Hey there!");
});



app.use(cors({ origin: "http://localhost:3000" }));

app.post("/stored", (req, res) => {
  console.log("its running 2: " + req.body);
  db.collection("quotes").insertOne(req.body, (err, data) => {
    if (err) return console.log(err);
    res.send("saved to db: " + data);
  });
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

Mongodb 类(db.js):

const mongoose = require("mongoose");

const connectDB = async () => {
  try {
    const conn = await mongoose.connect(process.env.MONGO_URI, {
      useUnifiedTopology: true,
      useNewUrlParser: true,
      // useCreateIndex: true,
      // useFindAndModify: false,
    });
    console.log(`MongoDB Connected : ${conn.connection.host}`);
  } catch (err) {
    console.error(err.message);
    process.exit(1);
  }
};
module.exports = connectDB;

我尝试将 fetch 从 https 更改为 http,然后出现以下两个错误:注意:第 148 行是 fetch 行在此处输入图像描述 在此处输入图像描述

如果您需要更多信息,请告诉我。

谢谢你。

首先在 postman 上测试你的 api。你可能应该从 mongoose 切换到 mongodb 包。Mongoose 的模型上没有 insertOne 方法。使用 modelName.create(req.body) 代替。 还设置一个模式,您可以从中获取模型

Fetch 可能正在尝试建立 SSL 到 localhost(因为您指定了https://localhost... ),但由于 localhost 没有证书而失败。 尝试执行http://localhost...并在其投入生产后将其切换到https://prod.whatever.com

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM