我有一个运行在后端的Node / Express服务器和一个响应前端,并且正在尝试制作最简单的API-但是无论我做什么,我似乎在发布请求中都收到错误404。我试图将一个代理添加到package.json中,删除该代理并将端口包括在地址中,即http://localhost:5000/createaccount,包括http://localhost:5000的代理,并在发布请求中仅使用'/ createaccount'。我已经尝试过使用CORS和标头进行各种操作,但似乎没有任何效果。

当我分别在浏览器中进入http://localhost:5000/createaccount时,它确实成功记录了“ hello world”,因此我知道服务器正在运行。我还知道,现在对发送的数据实际上什么也没做,只是尝试立即建立连接!我也尝试取消注释/注释掉提取请求中的大多数内容,但无济于事。

Server.js代码:

// server.js

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const PORT = 5000;
const cors = require("cors");

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

app.get("/createaccount", (req, res) => {
  console.log("test");
  res.append("Access-Control-Allow-Origin", ["*"]);
  res.append("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
  res.append("Access-Control-Allow-Headers", "Content-Type");
  res.send("testComplete");
});

app.listen(PORT, function() {
  console.log("Server is running on Port: ", PORT);
});


CreateAccountjs代码:

// Create.js

import React, { Component } from "react";
import axios from "axios";
import { Button, FormGroup, FormControl, FormLabel } from "react-bootstrap";

export default class CreateAccount extends Component {
  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.handleChange = this.handleChange.bind(this);

    this.state = {
      email: "",
      password: ""
    };
  }

  handleChange = e => {
    this.setState({
      [e.target.id]: e.target.value
    });
  };

  handleSubmit = e => {
    e.preventDefault();
    const data = {
      email: this.state.email,
      password: this.state.password
    };
    fetch("http://localhost:5000/createaccount", {
      method: "POST", // *GET, POST, PUT, DELETE, etc.
      mode: "cors", // no-cors, cors, *same-origin
      headers: {
        "Content-Type": "application/json"
        // "Access-Control-Allow-Origin": "*"
        // 'Content-Type': 'application/x-www-form-urlencoded',
      },
      redirect: "follow", // manual, *follow, error
      referrer: "no-referrer", // no-referrer, *client
      body: JSON.stringify(data) // body data type must match "Content-Type" header
    }).then(res => console.log(res));
  };

  render() {
    return (
      <div className="CreateAccount">
        <form onSubmit={this.handleSubmit.bind(this)}>
          <FormGroup controlId="email">
            <FormLabel>Email</FormLabel>
            <FormControl
              type="email"
              value={this.state.email}
              onChange={this.handleChange.bind(this)}
            />
          </FormGroup>
          <FormGroup controlId="password">
            <FormLabel>Password</FormLabel>
            <FormControl
              value={this.state.password}
              onChange={this.handleChange.bind(this)}
              type="password"
            />
          </FormGroup>
          <Button type="submit">Sign up!</Button>
        </form>
      </div>
    );
  }
}

最佳答案

您收到404,因为您没有在节点服务器中创建POST路由

所以你应该把它放在你的代码中

app.post("/createaccount", (req, res) => {

});


请让我知道我是否正确

10-07 21:47