填写登录凭据后,我试图登录到我的个人资料页面,但是我被重定向到http://localhost:3000/myprofile%20,但出现404错误。
这是我的代码的样子
//登录路径
router.post('/login', function(req, res, next){
if(req.body.email && req.body.password ){
Reg.authenticate(req.body.email, req.body.password, function(error, user){
if(error || !user){
var err = new Error("Wrong email or password");
err.status = 401;
return next(err);
}
else{
req.session.userId = user._id;
return res.redirect('/myprofile ')
}
});
}else{
var err = new Error("Email and Password required");
err.status = 401;
return next(err);
}
});
// GET /个人资料
router.get('/myprofile', function(req, res, next){
if(!req.session.userId){
var err = new Error("Please login with your email and password");
err.status = 403;
return next(err);
}
Reg.findById(req.session.userId)
.exec(function(error, user){
if(error){
return next(error);
}else{
return res.render('myprofile',{title:'My Profile', name:user.name, email:user.email, hobbies:user.hobbies,
address:user.address, medicalhistory:user.medicalhistory, allergies:user.allergies, gender:user.gender, bloodgroup:user.bloodgroup,
birthdate:user.birthdate, country:user.country, mobileNumber:user.mobileNumber})
}
})
});
答案 0 :(得分:2)
您的代码中还有一个空格:
router.post('/login', function(req, res, next) {
if (req.body.email && req.body.password) {
Reg.authenticate(req.body.email, req.body.password, function(error, user) {
if (error || !user) {
var err = new Error("Wrong email or password");
err.status = 401;
return next(err);
} else {
req.session.userId = user._id;
return res.redirect('/myprofile')
}
});
} else {
var err = new Error("Email and Password required");
err.status = 401;
return next(err);
}
});
将其删除,您将被重定向到正确的路由:
%%html
<style>
/* Jupyter */
.rendered_html table,
/* Jupyter Lab*/
div[data-mime-type="text-markdown"] table {
margin-left: 0
}
</style>
答案 1 :(得分:1)
在else部分中,您要将空格()附加到重定向功能
%20
是'
32
'空格的字符编码。您可能知道是'
0x20
的ASCII,而32
是Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "key", "$(document).ready(function(){'" + script + "'}", true);
的十六进制。
答案 2 :(得分:0)
返回时(在其他条件下)有一个空格-> return res.redirect('/myprofile ')
应该是这样的-> return res.redirect('/myprofile')