React给我重载时无法获取/ page_name(节点/表达)

时间:2019-05-14 06:20:05

标签: node.js reactjs express

我正在尝试构建一个reactjs应用,并且试图将数据从前端(反应)传递到后端(节点/表达式)。但是,当我尝试查看页面时出现错误。 (无法获取/ home)。

const express = require("express");
const app = express();
const port = 5000;

const cors = require("cors");
app.use(cors());
var bodyParser = require("body-parser");
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(
  bodyParser.urlencoded({
    // to support URL-encoded bodies
    extended: true
  })
);


app.post("/home", (req, res) => {
  const data = [(generalDetail = req.body.generalDetail)];
  console.log(generalDetail, "has been added to /home");

  res.json(data);
});

app.listen(port, () => `Server running on port ${port}`);

这是我的onSubmit函数:

  onSubmitForm = e => {
          e.preventDefault();
          let data = {
                generalDetail: this.state.generalDetails,
                firstName: this.state.firstName,
                middleName: this.state.middleName,
                lastName: this.state.lastName
          };

          axios.post("http://localhost:5000/home", data).then(() => {
             //do something
           }).catch(() => {
              console.log("Something went wrong. Plase try again later");

          });

1 个答案:

答案 0 :(得分:0)

您没有回家的路线,这就是您遇到麻烦的原因。 在您的发布路线上方添加以下代码。

app.get("/home", (req, res) => {
    console.log("here"); 
});