Search code examples
javascriptnode.jsexpressexpress-router

express router returns 404 on post method for one url


I have an express app to handle user authentication. My app.js file has app.use("/", indexRouter); app.use("/signup", signupRouter); app.use("/login", indexRouter); the signupRouter works perfect other than when I check if sign up successful and if not re render the signup page. Basically the css goes missing.

router.get("/", function(req, res) {
  res.render("signup", { message: "Fill out the form" });
});

router.post("/register", function(req, res) {
  db.createUser(email, name, password)
    .then(function() {
      res.redirect("/");
    })
    .catch(function(err) {
      console.log("something went wrong");
      res.render("signup", { message: `something went wrong ${err}` }); 
// here css goes missing even though my public folder is set to static.
    });
});

I have an indexRouter that is:

router.get("/", function(req, res) {
  res.render("login", { message: "Enter Credentials" });
});
router.post("/login", function(req, res) {
  db.findUser(email.password)
    .then(function() {
      res.redirect("/dashboard");
    })
    .catch(function(err) {
      res.render("login", { message: "USER/PASSWORD NOT FOUND" });
    });
});

Upon hitting this /login I get 500/400 errors. How come? My setup for both routers are identical.


Solution

  • Looks like some syntax error.
    In the indexRouter at line db.findUser(email.password), email.password seems the culprit.

    Shouldn't it be db.findUser(email, password)