![](/img/trans.png)
[英]Cannot login, and i getting POST http://localhost:5000/auth/login 404 (Not Found) and 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.