node.js中使用node-schedule实现定时任务实例


有的时候需要根据业务需要,晚上凌晨以后执行某些操作的时候,这个可能会有所帮助,我最近正在研究这个,欢迎共同探讨。

github地址:https://github.com/mattpat/node-schedule

一、安装


npm install node-schedule


二、确定时间,例如:2012年11月21日,5:30

var schedule = require('node-schedule');
var date = new Date(2012, 11, 21, 5, 30, 0);

var j = schedule.scheduleJob(date, function(){
console.log('The world is going to end today.');
});

取消预设计划

[code]
j.cancel();



三、每小时的固定分钟,例如:每个小时的42分钟

var schedule = require('node-schedule');

var rule = new schedule.RecurrenceRule();
rule.minute = 42;

var j = schedule.scheduleJob(rule, function(){
console.log('The answer to life, the universe, and everything!');
});


四、.一个星期中的某些天的某个时刻,例如:每周四,周五,周六,周天的17点

var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(4, 6)];
rule.hour = 17;
rule.minute = 0;

var j = schedule.scheduleJob(rule, function(){
console.log('Today is recognized by Rebecca Black!');
});

五、每秒执行


  var rule = new schedule.RecurrenceRule();

  var times = [];

  for(var i=1; i<60; i++){

    times.push(i);

  }

  rule.second = times;

  var c=0;
  var j = schedule.scheduleJob(rule, function(){
   c++;
  console.log(c);
  });

在Node.js中实现文件复制的方法和实例
Node.js本身并没有提供直接复制文件的API,如果想用Node.js复制文件或目录,需要借助其他的API来实现。复制单个的文件可以直接用readFile、writeFile,这样

nodejs文件操作模块FS(File System)常用函数简明总结
件系统操作相关的函数挺多的。首先可以分为两大类。一类是异步+回调的。一类是同步的。在这里只对异步的进行整理,同步的只需要在函数名称后面

NODE.JS加密模块CRYPTO常用方法介绍
使用require('crypto')调用加密模块。加密模块需要底层系统提供OpenSSL的支持。它提供了一种安全凭证的封装方式,可以用于HTTPS安全网络以及普通HTTP连接