从反应前端向节点后端发送请求时。获取 Login.jsx:38 请求失败,状态代码为 404

编程入门 行业动态 更新时间:2024-10-03 10:40:47

从反应前端向<a href=https://www.elefans.com/category/jswz/34/1771452.html style=节点后端发送请求时。获取 Login.jsx:38 请求失败,状态代码为 404"/>

从反应前端向节点后端发送请求时。获取 Login.jsx:38 请求失败,状态代码为 404

获取 Login.jsx:38 请求失败,状态码为 404 我的前端在反应中,后端在 express 中。两者都在相同的端口上运行。前端的应用程序 url 是正确的。我同时运行两个项目并尝试连接前面填写的用户信息以从后端签入以登录或发送回复。 这是我在前端处理提交功能

  const handleSubmit = async(event) => {
    event.preventDefault();
    // Handle login functionality here
console.log("next")

  try {
    const response = await axios.post(`${API_BASE_URL}/login`, {
      user: username,
      pwd: password
    });

    // Check if user is admin and redirect accordingly
    if (response.data.role === 'admin') {
      window.location.href = '/login';
    } else {
      window.location.href = '/login';
    }
   } catch (error) {
    console.error(error.message);
    // Handle error response
  }
  };

这是我在 express 中的终点

app.post("/login", async(req, res) => {
    const questionsFilePath = path.join(__dirname, "model", "questions.json");
    const questionsData = await fsPromises.readFile(questionsFilePath, "utf-8");
    const questions = JSON.parse(questionsData);
    // req.session.username = username;
    const { user, pwd } = req.body;
    if (!user || !pwd)
        return res.status(400).json({
            message: "Username and password are required.",
        });
    const foundUser = usersDB.users.find((person) => person.username === user);
    if (!foundUser)
        return res.status(409).render("login", {
            usrerror: "user not exists ",
        });
    const match = await bcryptpare(pwd, foundUser.password);
    if (!match) {
        return res.status(409).render("login", {
            pwderror: "wrong password ",
        });
    }

    //create jwt
    const accessToken = jwt.sign({
            username: foundUser.username,
            role: foundUser.role,
        },
        process.env.ACCESS_TOKEN_SECRET, { expiresIn: "9000s" }
    );
    const refreshToken = jwt.sign({
            username: foundUser.username,
            role: foundUser.role,
        },
        process.env.REFRESH_TOKEN_SECRET, { expiresIn: "1d" }
    );

    res.cookie("accessToken", accessToken, {
        httpOnly: true,
        sameSite: "None",
        secure: true,
        maxAge: 9000 * 1000,
    });
    res.cookie("refreshToken", refreshToken, {
        httpOnly: true,
        sameSite: "None",
        secure: true,
        maxAge: 24 * 60 * 60 * 1000,
    });
    // res.redirect("/questions");
    res.json({ role: foundUser.role });

});
回答如下:

更多推荐

从反应前端向节点后端发送请求时。获取 Login.jsx:38 请求失败,状态代码为 404

本文发布于:2024-05-30 20:53:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770911.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   后端   状态   代码   jsx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!