【发布时间】:2018-08-27 23:19:09
【问题描述】:
我想使用 NodeJS 和 MongoDB 创建一个正常运行时间监视器。我想在 NodeJS 中运行一个 cron 作业并将数据存储到 MongoDB 中。如果网站响应状态码不等于200,则将其保存在数据库中。我想做一个这样的数据库条目,
url : http://www.google.com
status_code : 500
start_time :- start time
end_time :- end time
我可以运行 cron 作业,但不确定如何在数据库中保存停机时间。因为,我不想将每个响应都存储到数据库中。只有当响应状态码不是200时,才会开始跟踪(start_time)URL,并将网站返回200的时间保持为end_time。
cron.js:-
var async=require('async');
const Entry = require('../models/health.model.js');
var https = require('https');
var request = require('request');
module.exports = function getHttpsRequests () {
Entry.find({},function(err,entrys){
console.log(err);
if(!err && entrys){
async.each(entrys,function(entry,callback){
request(entry.url, function (error, response, body) {
entry.statuscheck=response.statusCode;
entry.save();
callback();
});
},function (error) {
});
}
});
}
health.model.js:-
const mongoose = require('mongoose');
const EntrySchema = mongoose.Schema({
url: String,
statuscheck: String
}, {
timestamps: true
});
module.exports = mongoose.model('Entry', EntrySchema);
【问题讨论】:
-
您为什么要这样做而不是使用现有的正常运行时间监视器?我的意思不是对抗性的。我的意思是尝试从现有解决方案中提取您正在尝试做的事情的不同之处。
标签: javascript node.js mongodb mongoose