简体   繁体   English

如何纠正 ReactJS / NodeJS / MySQL 中的这个错误?

[英]How can I correct this error in ReactJS / NodeJS / MySQL?

Here is my App.js file (client side):这是我的 App.js 文件(客户端):

import "./App.css";
import { useState } from "react";
import Axios from "axios";

function App() {
  const [usernameReg, setUsernameReg] = useState("")
  const [passwordReg, setPasswordReg] = useState("")

  const register = () => {
    Axios.post("https://localhost3001/register", {
      username: usernameReg,
      password:passwordReg,
    }).then((response) => {
      console.log(response);
    });
  };

  return (
    <div className="App">
      <div className="information">
        <h1>Register</h1>
        <label>Name:</label>
        <input
          type="text"
          onChange={(e) => {
            setUsernameReg(e.target.value);
          }}
        />
        <label>Password:</label>
        <input
          type="text"
          onChange={(e) => {
            setPasswordReg(e.target.value);
          }}
        />
        <button onClick={register}>Register</button>
      </div>
      <div className="login">
        <h1>Login</h1>
        <label>Name:</label>
        <input
          type="text"
        />
        <label>Password:</label>
        <input
          type="text"
        />
        <button>Login</button>
      </div>
    </div>
  );
}

export default App;

and this is my index.js file (server side):这是我的 index.js 文件(服务器端):

const app = express();
const mysql = require("mysql");
const cors = require("cors");

app.use(cors());
app.use(express.json());

const db = mysql.createConnection({
  user: "x",
  host: "here is my db IP",
  password: "x",
  database: "x",
});

db.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

app.post("/register", (req, res) => {

  const username = req.body.username;
  const password = req.body.password;

  db.query(
    "INSERT INTO test (username, password) VALUES (?,?)",
    [username, password],
    (err, result) => {
        console.log(err);
    }
  );
});


app.listen(3001, () => {
  console.log("Yey, your server is running on port 3001");
});

When I start my React app, no problem into the console, same when I start my index.js (console prints "Yey, your server is running on port 3001" and "Connected." so there is no problem with the db connection), But when I press the register button: there is no data sent to my DB and I have these messages in the Chrome DevTools :当我启动我的 React 应用程序时,控制台没有问题,当我启动我的 index.js 时也是如此(控制台打印“是的,你的服务器正在端口 3001 上运行”和“已连接。”所以数据库连接没有问题) ,但是当我按下注册按钮时:没有数据发送到我的数据库,我在 Chrome DevTools 中有这些消息:

POST https://localhost3001/register net::ERR_CONNECTION_TIMED_OUT POST https://localhost3001/register net::ERR_CONNECTION_TIMED_OUT

and

Uncaught (in promise) Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:84)未捕获(承诺)错误:XMLHttpRequest.handleError (xhr.js:84) 处的 createError (createError.js:16) 网络错误

I have also in these DevTool (network window):我也有这些 DevTool(网络窗口):

Failed to load response data加载响应数据失败

What can I do to fix that please?请问我能做些什么来解决这个问题?

use http://localhost:3001/register .使用http://localhost:3001/register Not https://localhost3001/register不是https://localhost3001/register

From time to time we will make such silly mistakes:vvvv我们时不时会犯这样愚蠢的错误:vvvv

You should change this line:你应该改变这一行:

Axios.post("https://localhost3001/register", {

to this other对这个另一个

Axios.post("https://localhost:3001/register", {

You are missing a : to separate the url part (localhost) from the port 3001您缺少:将 url 部分(本地主机)与端口 3001 分开

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

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