cron与node-cron的作业不会在console.log中进行testing

我试图在我的node.js应用程序(一个只是为了这个工作)运行testingcron工作,但它不是console.log()应该是。 它应该每10秒console.log()但它不会。

这是我的目录/ app中除package.json文件和node_modules目录以外的唯一文件。

代码在这里:

 var express = require("express"), app = express(), bodyParser = require("body-parser"), mongoose = require("mongoose"), cron = require("node-cron"); mongoose.connect("mongodb://localhost/asset-management-v1"); // mongoose.connect("mongodb://localhost/yelp_camp_v10"); app.use(bodyParser.urlencoded({extended: true})); app.set("view engine", "ejs"); app.use(express.static(__dirname + "/public")); app.use(function(req, res, next){ res.locals.currentUser = req.user; next(); }); var task = cron.schedule('10 * * * *', function() { console.log('immediately started'); }, false); task.start(); 

我的最终目标是检查一个mongodb collection然后查看每个objectID ,如果objectID时间戳超过10天,则使用nodemailer发送电子邮件。 如果你看到除了造成console.log失败之外的任何东西,请指出,所以我不必再回来打扰你了。

10 * * * * – 表示“每10分钟”。 你需要10 * * * * *每10秒运行一次。

你可以通过true立即开始:

 var task = cron.schedule('10 * * * *', function() { console.log('immediately started'); }, true);