微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

GET 无法处理 Nodejs 中的重定向

如何解决GET 无法处理 Nodejs 中的重定向

所以,我在 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");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。