发送到客户端后无法设置标头

时间:2020-12-29 09:05:19

标签: node.js express

我正在开发登录尝试计数器并收到错误 Cannot set header after they are sent to the client。执行第一个和第二个“else if”条件时会出现问题。添加时:

.json({
  status: "failure",
  message: ""
});

通过单击我的 vue 前端上的登录按钮来触发条件来响应并执行代码。 最后一个条件完全没问题,即使是相同的响应也没有给我任何错误。

if (
  (lastTry[0].Time - firstTry[0].Time > 15000 && valid) ||
  (attempts[0].total <= 3 && valid)
) {
  console.log("DELETING AND LOGGING IN");
  await db.query("DELETE FROM loginattempts WHERE username=?", [
    req.body.username
  ]);
  await db.query("INSERT INTO loginattempts SET ?", [data]);

  let user = new User(compareUser[0]);
  const token = jwt.sign(
    {
      user
    },
    nconf.get("jwtToken"),
    {
      expiresIn: "14d"
    }
  );
  Object.assign(user, {
    token
  });
  res.json(user);
  // Check if period of time is over and if login attempt was not successful. Or if number of attempts is valid and login was not successfull.
  // If so, delete list of attempts and write current one back
} else if (lastTry[0].Time - firstTry[0].Time > 10000 && !valid) {
  console.log("DELETING AND NOT LOGGING IN");
  await db.query("DELETE FROM loginattempts WHERE username=?", [
    req.body.username
  ]);
  await db.query("INSERT INTO loginattempts SET ?", [data]);
  res.sendStatus(403).json({
    status: "failure",
    message: ""
  });
  // Check if attempts are fine and login was not successfull and push it to database
} else if (attempts[0].total < 3 && !valid) {
  // await db.query('INSERT INTO loginattempts SET ?', [data]);
  res.sendStatus(403).json({
    status: "failure",
    message: ""
  });
} else if (
  lastTry[0].Time - firstTry[0].Time < 10000 /*1800000*/ &&
  attempts[0].total > 3
) {
  // Check if difference of last and first login attempt is bigger than half an hour and for maximum of 3 login attempts during that period of time
  console.log("Wait half an hour!");
  res.status(403).json({
    status: "tooManyAttempts",
    message:
      "Zu viele Login-Versuche! Bitte in einer halben Stunde erneut probieren..."
  });
}

1 个答案:

答案 0 :(得分:3)

问题在于 sendStatus()。请注意以下两个差异:

  • status() 在响应上设置 HTTP 状态(作为服务器端的 Javascript 对象)。
  • sendStatus() 设置状态将其发送给客户端。

现在正确的做法是用 sendStatus() 替换所有的 status()

这是一个更新的代码:

if (
  (lastTry[0].Time - firstTry[0].Time > 15000 && valid) ||
  (attempts[0].total <= 3 && valid)
) {
  console.log("DELETING AND LOGGING IN");
  await db.query("DELETE FROM loginattempts WHERE username=?", [
    req.body.username
  ]);
  await db.query("INSERT INTO loginattempts SET ?", [data]);

  let user = new User(compareUser[0]);
  const token = jwt.sign(
    {
      user
    },
    nconf.get("jwtToken"),
    {
      expiresIn: "14d"
    }
  );
  Object.assign(user, {
    token
  });
  res.json(user);
  // Check if period of time is over and if login attempt was not successful. Or if number of attempts is valid and login was not successfull.
  // If so, delete list of attempts and write current one back
} else if (lastTry[0].Time - firstTry[0].Time > 10000 && !valid) {
  console.log("DELETING AND NOT LOGGING IN");
  await db.query("DELETE FROM loginattempts WHERE username=?", [
    req.body.username
  ]);
  await db.query("INSERT INTO loginattempts SET ?", [data]);
  res.status(403).json({
    status: "failure",
    message: ""
  });
  // Check if attempts are fine and login was not successfull and push it to database
} else if (attempts[0].total < 3 && !valid) {
  // await db.query('INSERT INTO loginattempts SET ?', [data]);
  res.status(403).json({
    status: "failure",
    message: ""
  });
} else if (
  lastTry[0].Time - firstTry[0].Time < 10000 /*1800000*/ &&
  attempts[0].total > 3
) {
  // Check if difference of last and first login attempt is bigger than half an hour and for maximum of 3 login attempts during that period of time
  console.log("Wait half an hour!");
  res.status(403).json({
    status: "tooManyAttempts",
    message:
      "Zu viele Login-Versuche! Bitte in einer halben Stunde erneut probieren..."
  });
}