【问题标题】:How to get the server time in the MEAN stack ? (MongoDB, Express, Angular, Node)如何在 MEAN 堆栈中获取服务器时间? (MongoDB、Express、Angular、节点)
【发布时间】:2017-02-06 19:24:23
【问题描述】:

代码:

exports.create = function (req, res) {
  var article = new Article(req.body);
  article.user = req.user;

  console.log("1) LAST: "+article.user.last.getTime());
  console.log("Date.now() "+Date.now());

  if (article.user.last != null && article.user.last != undefined) {
      console.log("1");
      console.log("DATE: "+(Date.now() - article.user.last));
      if ((Date.now() - article.user.last.getTime() > 1000 * 60 * 60)) {
            article.save(function (err) {
                if (err) {
                  return res.status(422).send({
                    message: errorHandler.getErrorMessage(err)
                  });
                } else {
                    res.json(article);

                    if (article.user) {
                        article.user.last = Date.now();
                        console.log("2) LAST: "+article.user.last.getTime());
                    } else {
                        res.status(401).send({
                          message: 'User is not signed in'
                        });
                    }
                }
            });
      }
      else {
          return res.status(422).send({
            message: "You need to wait 1 hour between Article creations or if you just created an account."
          });
      }
  }
  else {
      console.log("2");
      article.save(function (err) {
        if (err) {
          return res.status(422).send({
            message: errorHandler.getErrorMessage(err)
          });
        } else {
          res.json(article);

            if (article.user) {
                article.user.last = Date.now();
                console.log("3) LAST: "+article.user.last.getTime());
            } else {
                res.status(401).send({
                  message: 'User is not signed in'
                });
            }
        }
    });
  }
};

情况:

我不想使用Date.now(),而是使用服务器时间。

我的反垃圾邮件计时器可防止用户每小时发帖超过一次。

它工作正常,除了一件事:如果我使用 Date.now(),我可以通过简单地将本地时钟提前 1 小时来绕过计时器。

【问题讨论】:

  • Date.now() 在服务器上执行是 server 时间。 PS:从你的代码来看,这与 Angular 和 mongodb 无关。
  • @qqilihq 您在上面看到的代码是我的反垃圾邮件计时器。您需要在帖子之间等待 1 小时。如果在服务器上执行的 Date.now() 是服务器时间,那么为什么我能够将本地时钟提前 1 小时以绕过计时器?
  • @Coder1000 - 因为您在本地进行测试。你的机器时间就是服务器时间。
  • @tymeJV 脑洞大开:O
  • @tymeJV 写一个答案,这样我就可以接受了;)

标签: angularjs node.js mongodb


【解决方案1】:

当您在本地进行测试时(您的机器就是服务器),因此您将本地机器提前 1 小时的测试并不是真正有效的测试,因为它也将服务器时间提前了 1 小时。

Date.now() 是获取时间的正确方法,顺便说一句。我还想说,当您在这样的时间工作时,请记住使用 UTC。

【讨论】:

  • 太棒了!我认为 Date.now() 总是占用客户的时间。 TIL :D 谢谢!
  • 为什么选择 UTC? (我只是使用了默认的 Date.now(),是否需要进行更改?)
  • UTC 是日期的存储方式,以防您对时区进行更高级的操作。 Date.now() 可以正常工作 - 除非您的服务器意外切换时区,否则它可能会出现问题。读起来总是很有趣:yellerapp.com/posts/…
  • 谢谢!会看一下 :) (另外,你忘了 ping 我,幸运的是我在这里查看了 :D)
  • 好的,我读过了。这是我只需要在我的服务器(Google Cloud Compute Engine 上的虚拟机)上配置的设置,还是我必须在我的代码中设置的东西?
【解决方案2】:

使用 $currentDate

在此处查看文档: https://docs.mongodb.com/manual/reference/operator/update/currentDate/

【讨论】:

  • 不起作用。我尝试用 $currentDate 替换 Date.now() 。 “$currentDate 未定义”。我做错了什么?
  • 不是这样的。请阅读文档。由于您尚未在问题中编写完整的查询,因此无法提及如何在查询中使用 $currentDate。
猜你喜欢
  • 2013-12-24
  • 2013-11-17
  • 2023-03-24
  • 1970-01-01
  • 2014-07-08
  • 2018-12-21
  • 2018-03-19
  • 1970-01-01
  • 2016-01-06
相关资源
最近更新 更多