[英]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.