Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何将React表单数据发送到后端express服务器_Node.js_Express_Axios - Fatal编程技术网

Node.js 如何将React表单数据发送到后端express服务器

Node.js 如何将React表单数据发送到后端express服务器,node.js,express,axios,Node.js,Express,Axios,我制作了这个react表单,希望将候选对象发送到后端express服务器,在那里我希望控制台记录候选对象。我已经检查了表单是否正确输入了内容。我正在使用axios向express后端发送post请求 import React, { Fragment, useState } from "react"; import axios from "axios"; const Form = () => { const [candidate, setCand

我制作了这个react表单,希望将候选对象发送到后端express服务器,在那里我希望控制台记录候选对象。我已经检查了表单是否正确输入了内容。我正在使用axios向express后端发送post请求

import React, { Fragment, useState } from "react";
import axios from "axios";

const Form = () => {
  const [candidate, setCandidate] = useState({
    fullName: "",
    phoneNumber: 0,
    email: "",
    gitProfile: "",
    linkToResume: "",
    designation: "",
    interest: "",
  });

  const onChange = e =>
    setCandidate({ ...candidate, [e.target.name]: e.target.value });

  const onSubmit = e => {
    e.preventDefault();

    axios
      .post("http://localhost:5000/", {
        candidate,
      })
      .then(res => {
        console.log(res, candidate);
      });
  };
  const designationOptions = [
    "--select option--",
    "Graphic Designer",
    "Full Stack Developer",
    "Project Manager",
    "SEO and Digital Marketing",
  ];

  return (
    //form input code
  );
};

export default Form;

这是后端express服务器代码

const express = require("express"),
  bodyParser = require("body-parser");
(app = express()), (port = process.env.PORT || 5000), (cors = require("cors"));

app.use(
  cors({
    origin: "http://localhost:3000",
    credentials: true,
  })
);

app.use(bodyParser.json());
app.use(
  bodyParser.urlencoded({
    extended: true,
  })
);

app.get("/", function (req, res) {
  console.log(req.body);
});

app.listen(port, () => console.log(`Backend server live on ${port}`));

我想发送候选对象和console.log对象,但收到404错误。
我将此设置放在父目录下的两个不同文件夹中。

您使用的是
app.get
,同时使用axios作为
POST
发送请求。
尝试切换
app.get
app.post

这解决了我的问题。非常感谢你。我想将电子邮件凭据传递到同一目录中的另一个文件,以便进一步处理。你能帮我吗?