我在端口 9000 上使用 Express 应用程序作为后端,并在端口 3000 上使用 React 应用程序作为前端。
另外,我正在使用 Passport JS 进行身份验证。
passport.authenticate
中有一个选项,可以在用户身份验证成功与否时重定向用户。
我的问题是 Express 应用程序重定向到 /dashboard:9000 而不是 /dashboard:3000 (我的前端)。
如何更改 Express 中的重定向端口?
我的快速登录逻辑:
router.post("/login", passport.authenticate('local',
{ failureRedirect: '/login',
failureFlash: true }), function(req, res) {
if (req.body.remember) {
req.session.cookie.maxAge = 30 * 24 * 60 * 60 * 1000; // Cookie expires after 30 days
} else {
req.session.cookie.expires = false; // Cookie expires at end of session
}
res.redirect('/dashboard'); //redirect to dashboard
});
反应提交表单逻辑:
const submitForm = async (event) => {
axios
.post(
"http://localhost:9000/login",
{
"username":email,
"password":password,
"remember":shouldRemember
}
)
.then((res) => {
//Should I redirect using react router?
})
.catch((err) => {
console.log(err)
if(err.response.status == 401){
setFormError(true)
}
});
};
我尝试在快速应用程序重定向中对端口进行硬编码,但没有成功。
我也在使用cors。
我个人会使用 react-router 在客户端进行重定向,具体取决于 api 的响应