It has been a few days I have started learning Node JS.
我已经有几天开始学习Node JS了。
My Node JS application has a get api which fetches the books information from the MongoDB database in json format when the http://localhost:portnumber/mybooks url is fired.
我的Node JS应用程序有一个get api,当启动http://localhost:portnumber/mybooks url时,它将以json格式从MongoDB数据库中获取图书信息。
The book schema has four fields namely title, author, category and price. Now, I want to introduce a cron job which will run every 10th and 50th minute of every hour. It will check if there is any book with price more than 100 (currency does not matter here), it will delete that record (or document) from the database. Means it will run at 7:10AM, 7:50AM , then in the next hour at 8:10AM and 8:50AM and so on.
图书图式包括标题、作者、类别和价格四个领域。现在,我想介绍一种cron作业,它每小时每10分钟和50分钟运行一次。它将检查是否有任何价格超过100的书(在这里货币不重要),它将从数据库中删除该记录(或文档)。意思是它会在早上7点10分,7点50分,然后在下一个小时8点10分,8点50分等等。
I am starting my application with command ./bin/www from the application folder. However, I am not able to figure out how to implement this cron job service and where (in which file) to put this code so as to make it run at the above specified time whenever I start my application.
我正在从应用程序文件夹的命令./bin/www开始我的应用程序。但是,我不能确定如何实现这个cron作业服务,以及在哪里(在哪个文件中)将该代码放到该代码中,以便在我开始应用程序时,在上面指定的时间运行它。
I am including the bits of code of my so-far developed application here to let you have a look. Currently it is working fine for the get rest api.
我在这里包含了我迄今为止开发的应用程序的一些代码,让您看看。目前,它在get rest api中运行良好。
This is app.js:
这是app.js:
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
This is index.js :
这是指数。js:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Book1 = require('../models/mybook.model');
var db = 'mongodb://localhost/mybookdb';
var mongoose = require('mongoose');
mongoose.connect(db);
var conn = mongoose.connection;
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/mybooks', function(req, res) {
console.log('Showing all books');
Book1.find({})
.exec(function(err,records){
if(err){
res.send('error has occured');
}else{
console.log(records);
res.json(records);
}
});
});
module.exports = router;
and mybook.model as :
和mybook。模型为:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var BookSchema = new Schema({
title:String,
author:String,
category:String,
price: Number
});
module.exports = mongoose.model('Book', BookSchema);
Can anybody help me with how and in which file to implement the node-schedule cron to serve my requirement ?
谁能帮助我如何以及在哪个文件中实现节点调度cron以满足我的需求?
1 个解决方案
#1
2
I could figure out how to do it. I got the idea from https://www.npmjs.com/package/node-schedule.
我知道怎么做。我从https://www.npmjs.com/package/node-schedule得到了这个想法。
Although it is not an exact solution but near. In this case the cron job runs continuously at every moment and the deletion is not implemented though. The cron scheduler code could be put in the index.js as follows :
虽然这不是一个精确的解,但很接近。在这种情况下,cron作业每时每刻都在连续运行,但是没有执行删除操作。可以将cron调度程序代码放入索引中。js如下:
var s = require('node-schedule');
var j = schedule.scheduleJob('* * * * *', function() {
Book1.find({price : {$gt:100}} , function(err, result) {
if(err) {
console.log(err);
}
else {
if(!result) {
console.log("No book found");
} else {
console.log("Found a book with title "+result.title);
}
}
});
If anybody can complete this with the exact requirement would be helpful.Thanks.
如果有人能按要求完成,那就太好了。谢谢。
#1
2
I could figure out how to do it. I got the idea from https://www.npmjs.com/package/node-schedule.
我知道怎么做。我从https://www.npmjs.com/package/node-schedule得到了这个想法。
Although it is not an exact solution but near. In this case the cron job runs continuously at every moment and the deletion is not implemented though. The cron scheduler code could be put in the index.js as follows :
虽然这不是一个精确的解,但很接近。在这种情况下,cron作业每时每刻都在连续运行,但是没有执行删除操作。可以将cron调度程序代码放入索引中。js如下:
var s = require('node-schedule');
var j = schedule.scheduleJob('* * * * *', function() {
Book1.find({price : {$gt:100}} , function(err, result) {
if(err) {
console.log(err);
}
else {
if(!result) {
console.log("No book found");
} else {
console.log("Found a book with title "+result.title);
}
}
});
If anybody can complete this with the exact requirement would be helpful.Thanks.
如果有人能按要求完成,那就太好了。谢谢。