【问题标题】:GET not able to handle Redirect in NodejsGET 无法处理 Nodejs 中的重定向
【发布时间】:2021-04-26 08:17:49
【问题描述】:

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

});
});

我可以在控制台中看到打印了“该死”,但由于某种原因,重定向没有按照它应该去的那样转到以下 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("/*", function(req, 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>

请帮忙

【问题讨论】:

    标签: node.js post redirect get


    【解决方案1】:

    尝试使用

    res.redirect("/dashboard.html");

    【讨论】:

      猜你喜欢
      • 2013-05-07
      • 2014-05-19
      • 1970-01-01
      • 2022-01-23
      • 2023-03-08
      • 2014-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多