简体   繁体   English

在 reactjs nodejs axios POST http://localhost:5000/getData 404 (Not Found)

[英]POST http://localhost:5000/getData 404 (Not Found) in reactjs nodejs axios

I'm new to react and node.js.我是 React 和 node.js 的新手。 What I'm trying to do is I made a fake server(running on port 5000) with an API(http://localhost:5000/getData) having harcode value array of object.我想要做的是我制作了一个假服务器(在端口 5000 上运行),其 API(http://localhost:5000/getData)具有对象的硬编码值数组。 Now I want to add new object in the API(http://localhost:5000/getData) from react frontend(running on port 3000).现在我想从反应前端(在端口 3000 上运行)在 API(http://localhost:5000/getData)中添加新对象。 and for that I'm using post of axios but when I try to add new object it gives me error like this:为此,我正在使用 axios 的帖子,但是当我尝试添加新对象时,它给了我这样的错误:

在此处输入图片说明

Kinda confuse why it's givig like that i think i have done everything right?有点困惑为什么它像那样 givig 我认为我已经做对了一切? I'm attaching the relevent code.我正在附上相关代码。

server.js服务器.js

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

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


// Routes 
app.get('/getData', (req, res) => {
    res.json([
        {
            "description": "",
            "durationday": "fullday",
            "end": "null",
            "id": 0,
            "location": "",
            "start": "null",
            "title": "",
        },
        {
            "description": "yaaa!",
            "durationday": "absent",
            "end": "10-04-2021 12:10:00",
            "id": 1,
            "location": "perth",
            "start": "10-04-2021 12:10:00",
            "title": "holiday",
        },
        {
            "description": "busy",
            "durationday": "fullday",
            "end": "10-22-2021 12:10:00",
            "id": 2,
            "location": "melbourne",
            "start": '10-19-2021 12:00:00',
            "title": "working",
        },
        {
            "description": "dd",
            "durationday": "halfday",
            "end": "10-24-2021 12:10:00",
            "id": 3,
            "location": "updated perth",
            "start": "10-24-2021 12:10:00",
            "title": "updated holiday",
        }
    ]);
});



app.listen(5000, () => console.log("Running on pot 5000"))

React axios function反应 axios 函数

const sendData = async () => {
        const post = await axios.post('http://localhost:5000/getData', {

            description: "dd",
            durationday: "halfday",
            end: "10-24-2021 12:10:00",
            id: parseInt(344444),
            location: "updated perth",
            start: "10-24-2021 12:10:00",
            title: "updated holidayssssssssss",

        }
        )
            .then((response) => console.log(response.data))
            .catch((error) => console.log(error));

        return post;
    }

onClick button点击按钮

 <button onClick={sendData}> button post</button>

您的路线设置为get但您正在发送post

app.get('/getData', (req, res) => {
const post = await axios.post(

你通过GET做了getDate ,但是通过POST调用了它。

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

相关问题 POST http://localhost:3000/api/customers 404(未找到)reactjs nodejs - POST http://localhost:3000/api/customers 404 (Not Found) reactjs nodejs POST http://localhost:4200/ 404 未找到(从 Angular 到 Nodejs 的请求) - POST http://localhost:4200/ 404 Not found (Request from Angular to Nodejs) GET http://localhost:5000/错误 404(未找到)||无法连接到 API - GET http://localhost:5000/error 404 (Not Found)||Unable to connect to API React.js / Firebase 应用程序中的 Axios 错误(404):POST http://localhost:3000/login 404(未找到) - Axios error (404) in React.js / Firebase app: POST http://localhost:3000/login 404 (Not Found) 无法登录,我收到 POST http://localhost:5000/auth/login 404 (Not Found) 和 net::ERR_SSL_PROTOCOL_ERROR - Cannot login, and i getting POST http://localhost:5000/auth/login 404 (Not Found) and net::ERR_SSL_PROTOCOL_ERROR POST http://localhost:3001/[object%20Promise] 404 (Not Found) React Axios - POST http://localhost:3001/[object%20Promise] 404 (Not Found) React Axios POST http:// localhost:3000/404(未找到) - POST http://localhost:3000/ 404 (Not Found) 404 Not Found nodejs 到本地主机 - 404 Not Found nodejs to localhost ReactJS:GET http://localhost:8080/contact 404(未找到) - ReactJS: GET http://localhost:8080/contact 404 (Not Found) POST http://localhost:3000/undefined/post 404(未找到) - POST http://localhost:3000/undefined/post 404 (Not Found)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM