问题描述
所以,我在 NodeJS 中有这个 POST 方法来处理登录身份验证
// Login logic
app.post("/home",function(req,res){
Item.findOne({EmailID: req.body.emailAddress},function (err,docs) {
if(req.body.emailAddress === docs.EmailID && hash(req.body.password,docs.Salt) === docs.Password) {
currentUser = docs.FirstName;
console.log("damn");
res.redirect("dashboard.html");
}
});
});
我可以在控制台中看到打印了“damn”,但由于某种原因,重定向没有按照预期的方式转到以下 GET “dashboard.html” 方法,而是转到 404 错误功能
app.get("/dashboard.html",(req,res)=>{
console.log("here we go again");
Activity.find({},function(err,foundItems){
res.render("home",{newListItems: foundItems});
});
});
而是转到以下 404 函数。 “我们又来了”不会打印在控制台上。关于如何使重定向转到 get 函数的任何想法?
app.get("/*",res){
res.sendFile(__dirname + "/404.html")
});
这是我的 HTML
<form class="" action="/home" method="post">
<div class="">
<img class ="tent-image"src="assets/images/tent.png" alt width="90" height="72">
<h4 class="company">PASTIME</h4>
<h1 class="h3 mb-3 fw-normal"> Sign in</h1>
<h4 class="h3 fw-normal" style="color: <%= inputcolor %>"> <%= FirstLine %><br /> <%= SecondLine%> </h4>
<label for="inputEmail" class="virtually-hidden"></label>
<input id="inputEmail" name="emailID" class="form-control" placeholder="Email address" required="" autofocus="">
<label for="inputPassword" class="visually-hidden">Password</label>
<input type="password" name="passWORD" id="inputPassword" class="form-control" placeholder="Password" required="">
<div class="checkBox mb-3">
<label>
<input type="checkBox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-dark" type="submit">Submit</button>
<p class="mt-5 mb-3 text-muted">© 2021 Pastime</p>
</form>
请帮忙
解决方法
尝试使用
res.redirect("/dashboard.html");