Node.js 在我的ReactJS项目上设置Express JS时API出现问题

Node.js 在我的ReactJS项目上设置Express JS时API出现问题,node.js,reactjs,express,Node.js,Reactjs,Express,我在这里做一个小项目,在连接“注册”和“登录”后遇到了一个问题 我是通过Express的NodeJS完成的 我的错误是: Access to XMLHttpRequest at 'api' from origin 'http://localhost:3000' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the

我在这里做一个小项目,在连接“注册”和“登录”后遇到了一个问题

我是通过Express的NodeJS完成的

我的错误是:

Access to XMLHttpRequest at 'api' from origin 'http://localhost:3000' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
每当我尝试从API获取产品时,控制台中就会出现。如果我没有登录到某个帐户,则不会出现此错误,但如果我登录到了某个帐户,则会发生此错误

这是我的Nodejs代码:

const express = require("express");
const mysql = require("mysql");
const cors = require("cors");

const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const session = require("express-session");

const bcrypt = require("bcrypt");

const saltRounds = 10;

const app = express();

app.use(
  cors({
    origin: ["http://localhost:3000"],
    methods: ["GET", "POST"],
    credentials: true,
  })
);

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

app.use(
  session({
    key: "",
    secret: "",
    resave: false,
    saveUninitialized: false,
    cookie: {
      expires: 60 * 60 * 24,
    },
  })
);

app.use(express.json());

const db = mysql.createConnection({
  user: "",
  host: "",
  password: "",
  database: "",
});

app.post("/register", (req, res) => {
  const firstName = req.body.firstName;
  const lastName = req.body.lastName;
  const username = req.body.username;
  const phone = req.body.phone;
  const password = req.body.password;
  const email = req.body.email;

  bcrypt.hash(password, saltRounds, (err, hash) => {
    db.query(
      "INSERT INTO `accounts`(`firstName`, `lastName`, `username`, `password`, `email`, `phone`) VALUES (?,?,?,?,?,?)",
      [firstName, lastName, username, hash, email, phone],
      (err, result) => {
        console.log(err);
        console.log(result);
      }
    );
  });
});

app.get("/login", (req, res) => {
  if (req.session.user) {
    res.send({ loggedIn: true, user: req.session.user });
  } else {
    res.send({ loggedIn: false });
  }
});

app.post("/login", (req, res) => {
  const email = req.body.email;
  const password = req.body.password;

  let sql = `SELECT * FROM accounts WHERE email="${email}"`;
  db.query(sql, (err, result) => {
    try {
      bcrypt.compare(password, result[0].password, (err, compResult) => {
        if (err) throw err;
        if (compResult) {
          req.session.user = result;
          res.send(compResult);
        } else if (!compResult) {
          res.send({ message: "Incorrect Password or Email!" });
        }
      });
    } catch (err) {
      res.send({ message: "Email does not exist!" });
    }
  });
});

如果您还需要什么,请告诉我

代码看起来不应该发送通配符。可能您以前做过
*
更改,但后来没有重新启动服务器?没有,我尝试过多次重新启动服务器,但没有结果。我使用的API来自
RapidAPI
(具体来说,是ASOS API)。不过,在提取数据的同一个文件中,我尝试添加:
Axios.defaults.withCredentials
,但没有将其设置为true,而是将其设置为false。它似乎工作,但我不100%确定它甚至做什么,它会把我的登录/注册的东西搞砸吗@QuentinTurning credentials support off意味着它是一个简单的请求,而不是一个预处理的请求,它允许
*
,但不允许cookies。啊,好的,我的理解是。我这种情况下的饼干是我的“会话”吧?(过去只处理烧瓶和登记表,那里简单多了哈哈)。所以我技术上不希望它是假的?或者这真的不重要吗?这是一个学校项目,因此只有我的老师会使用它。Cookies(在您的示例中)用于将登录的浏览器与服务器上的数据集合链接。如果它们没有被接受并发送回来,那么登录系统将无法工作。它将显示“您已登录!”然后在下一页上忘记您是谁。