简体   繁体   English

MERN 堆栈中的代理错误:无法将请求 /contact 从 localhost:3000 代理到 http://localhost:5000

[英]Proxy error in MERN stack: Could not proxy request /contact from localhost:3000 to http://localhost:5000

I know this question has been asked earlier but none of the solutions are working for me我知道之前有人问过这个问题,但没有一个解决方案对我有用

Complete error log:完整的错误日志:

  1. Shown on client side在客户端显示
Proxy error: Could not proxy request /contact from localhost:3000 to http://localhost:5000/.
See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNRESET).
  1. Shown on the server side network tab显示在服务器端网络选项卡上
Proxy error: Could not proxy request /contact from localhost:3000 to http://localhost:5000/ (ECONNRESET).
  1. Shown in console在控制台中显示
POST http://localhost:3000/contact 500 (Internal Server Error)
Uncaught (in promise) SyntaxError: Unexpected token P in JSON at position 0

The solutions tried尝试的解决方案

Package.json client side Package.json 客户端

  "name": "client",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://localhost:5000",
  "dependencies": {
    "@testing-library/jest-dom": "^5.14.1",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "framer-motion": "^4.1.17",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.1.2"
  },
}

package.json server side package.json 服务器端

  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "cookie-parser": "^1.4.5",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "http-proxy-middleware": "^2.0.1",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.13.2"
  }
}

server side files服务器端文件

auth.js (main router file aka server.js) auth.js(主路由器文件又名 server.js)

router.get("/getdata", authenticate, (req, res) => {
  res.send(req.rootUser);
});

router.post("/contact", authenticate, async (req, res) => {
  console.log("in auth.js");
  try {
    const { name, email, phone, msg } = req.body;

    if (!name || !email || !phone || !msg) {
      console.log("fill all fields");
      return res.json({ error: "Fill all fields" });
    }

    //req.userID define in authenticate
    else {
      const userExists = await User.findOne({ _id: req.userId });

      if (userExists) {
        const userContact = await userExists.addMessage(name,email,phone,msg);
        await userExists.save();
        res.status(201).json({ message: "Message sent" });
      }
    }
  } catch (error) {
    console.log(error);
  }
});

userschema.js (model file for the database, only added the required code here instead of full code) usersschema.js(数据库的模型文件,这里只添加了需要的代码而不是完整代码)

userSchema.methods.addMessage = async function (name, email, phone, msg) {
  try {
    this.messages = this.messages.concat({
      name,
      email,
      phone,
      msg,
    });
    await this.save();
    return this.messages;
  } catch (error) {
    console.log(error);
  }
};

client side file - about.js客户端文件 - about.js


const Contact = () => {
  const [userData, setUserData] = useState({name: "",email: "",phone: "",msg: "",});

  const getuserData = async () => {
    try {
      const res = await fetch("/getdata", {
        method: "GET",
        headers: {
          "Content-Type": "application/json",
        },
      });

      const data = await res.json();

      setUserData({
        ...userData,
        name: data.name,
        email: data.email,
        phone: data.phone,
      });

      if (!res.status === 200) {
        const error = new Error(res.error);
        throw error;
      }
    } catch (error) {
      console.log(error);
    }
  };

  useEffect(() => {
    getuserData();
  }, []);

  const handleInput = (e) => {
    const key = e.target.name;
    const value = e.target.value;
    setUserData({ ...userData, [key]: value });
  };

  const sendMessage = async (e) => {
    console.log("in func");
    e.preventDefault();
    const { name, email, phone, msg } = userData;

    const res = await fetch("/contact", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        name,
        email,
        phone,
        msg,
      }),
    });

    const data = await res.json();

    if (!data) {
      console.log("msg from contact page not sent");
    } else {
      alert("Message Sent!");
      setUserData({ ...userData, msg: "" });
    }
  };

我通过在 http://localhost:500 之后放置 / 来解决它,正确的方法是

"proxy": "http://localhost:5000/"

暂无
暂无

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

相关问题 代理错误:无法代理请求 /api/house-listing 从 localhost:3000 到 http://localhost:5000? (ECONNRESET)、MERN - Proxy error: Could not proxy request /api/house-listing from localhost:3000 to http://localhost:5000? (ECONNRESET), MERN 代理错误:无法代理请求/用户从 localhost:3000 到 http://localhost:5000/ - Proxy error: Could not proxy request /users from localhost:3000 to http://localhost:5000/ 出现错误:无法将请求/ api / plaid / accounts从localhost:3000代理到http:// localhost:5000 / - getting error : Could not proxy request /api/plaid/accounts from localhost:3000 to http://localhost:5000/ “React 代理错误:无法将请求 /api/ 从 localhost:3000 代理到 http://localhost:5000 (ECONNREFUSED)”?错误.. 没有在线解决方案 - "React Proxy error: Could not proxy request /api/ from localhost:3000 to http://localhost:5000 (ECONNREFUSED)'? Error.. No solution online 代理错误:无法将请求/付款从 localhost:3000 代理到 https://localhost:5000/ - Proxy error: Could not proxy request /payment from localhost:3000 to https://localhost:5000/ 无法从 localhost:3000 代理请求 /pusher/auth 到 http://localhost:5000 (ECONNREFUSED) - Could not proxy request /pusher/auth from localhost:3000 to http://localhost:5000 (ECONNREFUSED) React 代理错误:无法代理请求 /api/ 从 localhost:3000 到 http://localhost:8000 (ECONNREFUSED) - React Proxy error: Could not proxy request /api/ from localhost:3000 to http://localhost:8000 (ECONNREFUSED) 代理错误:无法将请求/用户从localhost:3000代理到http:// localhost:3001 / - Proxy error: Could not proxy request /users from localhost:3000 to http://localhost:3001/ 代理错误:无法将请求 /adduser 从 localhost:3000 代理到 http://127.0.0.1/4000 - Proxy error: Could not proxy request /adduser from localhost:3000 to http://127.0.0.1/4000 无法将请求/ api / user从localhost:3000代理到http:// localhost:3000 / - Could not proxy request /api/user from localhost:3000 to http://localhost:3000/
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM