在NodeJS中动态获取条目

时间:2018-07-13 11:14:22

标签: javascript node.js mongodb

我正在使用MongoDB和NodeJS。我有两个文件,models.jsstatus.js

models.js:-

const mongoose = require('mongoose');

const EntrySchema = mongoose.Schema({
    url: String,
    statuscheck: String
}, {
    timestamps: true
});

module.exports = mongoose.model('Entry', EntrySchema);

status.js:-

const Entry = require('models.js');

var https = require('https');

module.exports = function getHttpsRequests () {

    https.get('https://www.google.com', function (res) {
        console.log("statusCode: ", res.statusCode);

    }).on('error', function (e) {
        console.error(e);
    });
}

这是我的MongoDB数据的样子,

[
    {
        "_id": "1",
        "url": "https://google.com/",
        "statuscheck": "200",
        "createdAt": "2018-07-13T10:20:36.695Z",
        "updatedAt": "2018-07-13T10:20:36.695Z",
        "__v": 0
    },
    {
        "_id": "2",
        "url": "https://facebook.com/",
        "statuscheck": "200",
        "createdAt": "2018-07-13T10:21:20.699Z",
        "updatedAt": "2018-07-13T10:21:20.699Z",
        "__v": 0
    },
    {
        "_id": "3",
        "url": "https://wikipedia.com/",
        "statuscheck": "200",
        "createdAt": "2018-07-13T10:21:27.270Z",
        "updatedAt": "2018-07-13T10:21:27.270Z",
        "__v": 0
    },
    {
        "_id": "4",
        "url": "https://twitter.com/",
        "statuscheck": "200",
        "createdAt": "2018-07-13T10:23:12.271Z",
        "updatedAt": "2018-07-13T10:23:12.271Z",
        "__v": 0
    },
    {
        "_id": "5",
        "url": "https://ebay.com/",
        "statuscheck": "200",
        "createdAt": "2018-07-13T10:23:23.891Z",
        "updatedAt": "2018-07-13T10:23:23.891Z",
        "__v": 0
    }
]

在status.js中,我正在手动检查URL(通过对google.com进行硬编码)。在这一行,

https.get('https://www.google.com', function (res) { 

我正在尝试从Entry获取条目,并以for循环方式动态放置URL(存在于MongoDB数据中的URL)。这意味着,它应该检查所有存在的URL,而不只是google.com

我要尝试做的另一件事是针对该特定statuscheck动态更新url键值。每次,它都会检查URL,同时也会更新数据库中的statuscheck值。

2 个答案:

答案 0 :(得分:0)

使用异步npm:https://caolan.github.io/async/docs.html

请求npm:https://www.npmjs.com/package/request

Entry.find({})将返回数组...。然后我们必须遍历每个条目,并发出http get请求...。因此我们在each循环中使用了异步npm模块,异步运行并进行http调用。

var async=require('async');
const Entry = require('models.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) {
            console.log('error:', error); 
            console.log('statusCode:',response.statusCode); 
            entry.statusCode=res.statusCode;
            entry.save();
            callback();
            });


         },function (error) {

         });

      }
    });

}

答案 1 :(得分:0)

将您的代码分成多层,这样至少您有一个清晰的主意。

您的DAO应该是:

const Entry = require('models.js');

function getAll(callback){
   Entry.find({}).exec(function (err, data) {
      callback(err, data);
   });
}


function update(id, status, callback){
   Entry.findOneAndUpdate({
     _id: id
   }, {
      $push: {statuscheck: status}
   }).exec(function(err, update){
      console.log(update);
      callback(err, update);
   });
}

您的for循环应在status.js下:

DAO.getAll(function(err, data){
    for(var i =0; i< data.length; i++){
        checkAndUpdate(data[i].url, data[i].id);
    }

});

// This is seperate function to prevent "i" value to be overridden as your dependent on third party.
function checkAndUpdate(url, id){
    https.get(url, function (res) {
        DAO.update(id, res.statusCode, function(err, data){
            console.log("updated");
        });
    }).on('error', function (e) {
        console.error(e);
    });
}