Getting 504 gateway timeout error in Nodejs

546 views Asked by At

I have been getting 504 gateway time out error after around 30-40 secs while logging in the front end website. first I thought it must be MongoDB error as Mongoose new version have some changes like findOne do not take any callbacks, though I fixed that error. But still, I am getting this 504 error in production. I checked my endpoints and it is working fine in development, I am able to login/signup using Postman. I even put try..catch in every endpoints for the error.

I am using express with MongoDB/Mongoose and my hosted the backend in AWS EC2. I am getting this following error in my console log.

Object { .... message: "Request aborted", name: "AxiosError", code: "ECONNABORTED", config: {…}, request: XMLHttpRequest }
​
code: "ECONNABORTED"
​
config: Object { timeout: 0, xsrfCookieName: "XSRF-TOKEN", xsrfHeaderName: "X-XSRF-TOKEN", … }
​
message: "Request aborted"
​
name: "AxiosError"
​
request: XMLHttpRequest { readyState: 0, timeout: 0, withCredentials: true, … }
​....```

I usually getting this error while logging and signing up. My login, signup and OAuth endpoints are:

  1. Login
router.post("/login", (req, res) => {
  try {
    const user = new User({
      username: req.body.username,
      password: req.body.password,
    });
    console.log("/login", user);
    req.login(user, async function (err) {
      if (err) {
        console.error("Error during login:", err);
        res.redirect("/register");
      } else {
        await passport.authenticate("local", { failureRedirect: "/redirect" })(
          req,
          res,
          function () {
            console.log("logged in");
            res.redirect("/redirect");
          }
        );
      }
    });
  } catch (error) {
    console.log(error);
    return res.status(500).json({ Loginmessage: error });
  }
});
  1. Signup(or register)
router.post("/register", (req, res) => {
  try {
    User.register(
      {
        username: req.body.username,
      },
      req.body.password,
      function (err, user) {
        if (err) {
          console.log("/register err", err);
          res.redirect("/redirect");
        } else {
          passport.authenticate("local", {
            failureRedirect: process.env.SELF_URL + "/signup",
          })(req, res, function () {
            console.log("signed up");
            //   res.redirect("/redirect");
            res.redirect("/redirect");
          });
        }
      }
    );
  } catch (error) {
    console.log(error);
    return res.status(500).json({ Signupmessage: error });
  }
});
  1. Redirect Endpoint
router.get("/redirect", async function (req, res) {
  if (req.isAuthenticated()) {
    await User.findOne({ username: req.user.username })
      .exec()
      .then(async (result) => {
        if (result.verification === true) {
          res.redirect(process.env.REACT_URL + "/account");
        } else {
          const uid = new shortId();
          const token = uid.stamp(32);
          await User.findOneAndUpdate(
            { username: req.user.username },
            { otp: token },
            { new: true }
          )
            .then(async (result) => {
              console.log(result.otp);
              // res.redirect(process.env.REACT_URL + "/verifyEmail");
              const Transporter = nodemailer.createTransport({
                service: "gmail",
                auth: {
                  user: process.env.GMAILID,
                  pass: process.env.GMAILAPPPASSWORD,
                },
                from: process.env.GMAILID,
              });
              const mailOptions = {
                from: process.env.GMAILID,
                to: req.user.username,
                subject: "Verify Email, United Protocol",
                text: "Verify your email-id by clicking on the below link!!",
                html: MY_HTML_CODE,
              };

              await Transporter.sendMail(mailOptions)
                .then((response) => {
                  res.redirect(process.env.REACT_URL + "/verify");
                })
                .catch((err) => {
                  console.log(err);
                });
            })
            .catch((err) => {
              console.log("catch err at /redirect mail", err);
              res.redirect(process.env.REACT_URL + "/login");
            });
        }
      })
      .catch((err) => {
        console.log("catch err at /redirect (req.isAuthenticated())", err);
        res.redirect(process.env.REACT_URL + "/login");
        return res.status(500).json({ redirectmessage: error });
      });
  } else {
    res.redirect(process.env.REACT_URL + "/login");
  }
});

I've tried

  1. increase the timeout value in my back-end
var server = app.listen(5000, () => {   console.log("Server started on port 5000"); });  
server.timeout = 120000;
  1. tried to create a new mongodb database

  2. checked endpoints in development using Postman

I really not able to understand that is this a back-end issue or the server issue.

0

There are 0 answers