node-schedule的使用说明

这篇笔记内容很简单,就只是记录一下node-schedule的使用方法,特别是在初始化一个定时任务时,需要传递的参数的写法含义。

先看一段代码:

1
2
3
4
5
6
7
8
9
var schedule = require('node-schedule');

function createScheduleCron(){
schedule.scheduleJob('30 * * * * *', function(){
console.log('excute scheduleCronstyle:' + new Date());
});
}

createScheduleCron();

Read More

node.js中module.exports与exports的区别

我们在阅读它人或开源的node.js代码时,会发现在对模块导出的写法上会出现下面几种情况,而关于导出的方法整理,可以参见我这篇文章node.js中exports的用法整理。现在,我们以导出字符串对象为例:

在module.js文件中,有如下写法:

  • 1.exports.get_name = ‘fungwan by exports ‘;
  • 2.module.exports.get_name = ‘fungwan by modules’;
  • 3.module.exports = ‘fungwan by modules’;

通过对比发现,这里较之前多了module.exports,那么它跟exports的导出有什么关系呢?查阅资料即可知晓,其实exports就是module.exports对象的引用,本质上是相等的。我们可以通过这句代码证实:

Read More

node.js中exports的用法整理

相信很多跟我一样是node.js新手的同学对模块的导出或多或少有些混淆,现在我把exports对象的方式整理一下,其中包含了模块中需要导出的string对象、json对象、函数、类对象。

我们将需要导出的模块就命名为module.js,通过下面的代码导出需要的对象:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
exports.some_words = 'this is string';//导出一个字符串

exports.some_fun = function() {//导出一个函数
console.log('this is function');
};

exports.jsonObj = {
name : 'fungwan',
sex : 'man',
run : function(){
console.log('love running!');
}
};

//导出一个类
function helper() {
var number = '';
this.do = function () {
console.log('I can help you!');
}
}

helper.prototype.giveMoney = function(){
console.log('I have lots of cash!');
}

exports.c_help = helper;

Read More