节点后端发送请求时。获取 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
发布评论