简体   繁体   English

无法将 Axios 的 404 错误发布到 Create React App 上的 Express 服务器

[英]Cannot POST 404 error with Axios to Express server on Create React App

I'm using node/express on the backend and react on the frontend.我在后端使用 node/express 并在前端做出反应。

I'm testing a post request to 'http://localhost:8080/users/register' but on the client side I get a 404 error:我正在测试对“http://localhost:8080/users/register”的发布请求,但在客户端出现 404 错误:

POST http://localhost:3000/ 404 (Not Found)

Postman post request (This works fine) Postman 发布请求(这工作正常)

http://localhost:8080/users/register

I don't know why as I've set the proxy in my client side package.json file to point to my server port.我不知道为什么,因为我在客户端 package.json 文件中设置了代理以指向我的服务器端口。

Package.json which includes proxy Package.json 包括代理

      "browserslist": {
        "production": [
          ">0.2%",
          "not dead",
          "not op_mini all"
        ],
        "development": [
          "last 1 chrome version",
          "last 1 firefox version",
          "last 1 safari version"
        ]
      },
      "proxy": "http://localhost:8080"
    }

Axios POST request Axios POST 请求

import React from 'react';
import axios from 'axios';

function Register() {
    function onHandleRegisterSubmit() {
    axios.post('/users/register', {
    name: 'Joe',
    email: 'joe@msn.com'
  })
      .then((response) => {
        console.log(response);
      })
      .catch((error) => {
        console.log(error);
      });
    }

You don't have base URL "localhost:8000" while making the request, change request URL to localhost:8000/users/register您在发出请求时没有基础 URL "localhost:8000",将请求 URL 更改为 localhost:8000/users/register

Explanation:解释:

If you don't provide base URL then the request will be made to the base URL of the front end, that's why you're getting 404 and if you notice there is mentioned localhost:3000如果您不提供基础 URL 则请求将发送到前端的基础 URL ,这就是您得到 404 的原因,如果您注意到提到了 localhost:3000

A better way to do it: If you have multiple requests in your project then you should create an instance of axis and use that instance更好的方法:如果您的项目中有多个请求,那么您应该创建一个轴实例并使用该实例

This should not be a problem for anyone anymore.这应该不再是任何人的问题。 Kindly, on your backend, return a single response for both Postman and axios, then check if both responses are the same.请在您的后端,为 Postman 和 axios 返回一个响应,然后检查两个响应是否相同。 Then have a close look at your logic inside the posted function: Have a look at the below example:然后仔细看看你在发布的 function 中的逻辑:看看下面的例子:

export const signin = async (req, res, next) => {

return res.status(200).json('Everthing is okay!')

// try {
//     const user = await User.findOne({ email: req.body.email })
//     if (!user) return next(createError(200, 'User not found.'))

//     const isCorrect = await bcrypt.compare(req.body.password, user.password)

//     if (!isCorrect) return next(createError(200, 'Wrong Credentials.'))

//     const token = jwt.sign({ id: user._id }, process.env.JWT)

//     const { password, ...others } = user._doc

//     res.cookie("access_token", token, {
//         httpOnly: true
//     }).status(200).json(others)

// } catch (err) {
//     // throw
//     next(err)
// }

}

if not then from your front-end make full url request not using proxy eg:如果不是,那么从您的前端发出完整的 url 请求,而不使用代理,例如:

http://localhost:8800/api/auth/sign-in

as opposed to

/api/auth/sign-in

If these 2 fail, then you cloud be having a different issue on your front-end other than the backend.如果这两个都失败了,那么您的前端可能会遇到与后端不同的问题。

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

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