Create Node JS Server that stores and displays data based on the day of the week

前端 未结 2 355
情书的邮戳
情书的邮戳 2021-01-29 08:28

I need to create a function that can display a metric pulled at different hours of the day from an outside source one week ago. The way I have my server currently set up is usin

相关标签:
2条回答
  • 2021-01-29 08:48

    Did you try use an external module, like node-schedule? The periodic issues may be easier with it. With this module you may, for example, execute some task periodically, setting the intervall you want, say, 1 week.

    Another point to consider is the timezone. It may be a problem when you commit the code to server in another tz and it fails your counts. moment-timezone module is a good resource.

    0 讨论(0)
  • 2021-01-29 09:10

    Something along these lines might help. Applying node cron to set the interval:

    var net = require('net');
    
    var port = Number(process.env.PORT || 3000)
    
    var timeInMs = Date.now();
    var weekInMs = 604800000;
    
    var server = net.createServer(function (socket) {
        //if time in Ms is time from first ping to now then... 
        socket.end(timeInMs + '\r\n');
    });
    
    server.listen(port);
    

    The cron job:

    var CronJob = require('cron').CronJob;
    new CronJob('* * * * * *', function() {
      console.log('You will see this message every second');
    }, null, true, 'time zone');
    
    0 讨论(0)
提交回复
热议问题