经过Node.js的官方API能够看到Node.js自己提供了不少核心模块 http://nodejs.org/api/ javascript
(本次主要说自定义模块)html
Node.js还有一类模块为文件模块,能够是 JavaScript 代码文件(.js做为文件后缀)、也能够是 JSON 格式文本文件(.json做为文件后缀)、还能够是编辑过的 C/C++ 文件(.node做为文件后缀);java
文件模块访问方式经过require('/文件名.后缀') ; require('./文件名.后缀') ; requrie('../文件名.后缀') 去访问,文件后缀能够省略;以"/"开头是以绝对路径去加载,以"./"开头和以"../"开头表示以相对路径加载,而以"./"开头表示同级目录下文件,node
前面提到文件后缀能够省略,Nodejs 尝试加载的优先级 js文件 > json文件 > node文件json
以一个计数器为例api
var outputVal = 0; //输出值 var increment = 1; //增量 /* 设置输出值 */ function seOutputVal (val) { outputVal = val; } /* 设置增量 */ function setIncrement(incrementVal){ increment = incrementVal; } /* 输出 */ function printNextCount() { outputVal += increment; console.log(outputVal) ; } function printOutputVal() { console.log(outputVal); } exports.seOutputVal = seOutputVal; exports.setIncrement = setIncrement; module.exports.printNextCount = printNextCount;
示例中重点在于exports和module.exports;提供了外部访问的接口,下面调用一下看看效果吧数组
/* 一个Node.js文件就是一个模块,这个文件多是Javascript代码、JSON或者编译过的C/C++扩展。 重要的两个对象: require是从外部获取模块 exports是把模块接口公开 */ var counter = require('./1_modules_custom_counter'); console.log('第一次调用模块[1_modules_custom_counter]'); counter.seOutputVal(10); //设置从10开始计数 counter.setIncrement (10); //设置增量为10 counter.printNextCount(); counter.printNextCount(); counter.printNextCount(); counter.printNextCount(); /* require屡次调用同一模块不会重复加载 */ var counter = require('./1_modules_custom_counter'); console.log('第二次调用模块[1_modules_custom_counter]'); counter.printNextCount();
运行能够发现经过 exports 和 module.exports 对外公开的方法均可以访问!缓存
示例中能够看到,我两次经过require('./1_modules_custom_counter')获取模块,可是第二次引用后调用 printNextCount()方法确从60开始~~~app
缘由是node.js经过require,require屡次调用同一模块不会重复加载,Node.js会根据文件名缓存全部加载过的文件模块,因此不会从新加载了ide
注意:经过文件名缓存是指实际文件名,并不会由于传入的路径形式不同而认会是不一样的文件
在我建立的1_modules_custom_counter文件中有一个printOutputVal()方法,它并无经过exports或module.exports提供对外公开访问方法,
若是1_modules_load文件中直接访问运行会出现什么样的状况呢?
答案是:TypeError: Object #<Object> has no method 'printOutputVal'
exports 是指向的 module.exports 的引用
module.exports 初始值为一个空对象 {},因此 exports 初始值也是 {}
require() 返回的是 module.exports 而不是 exports
API提供了解释 http://nodejs.org/api/modules.html
Note that exports is a reference to module.exports making it suitable for augmentation only. If you are exporting a single item such as a constructor you will want to use module.exports directly instead exports 仅仅是 module.exports 的一个地址引用。nodejs 只会导出 module.exports 的指向,若是 exports 指向变了,那就仅仅是 exports 不在指向 module.exports,因而不会再被导出
原文:http://www.hacksparrow.com/node-js-exports-vs-module-exports.html
参考:http://www.cnblogs.com/kongxianghai/p/3936197.html
http://www.cnblogs.com/zhongweiv/p/nodejs_module.html
1. nodejs 模块中的 exports 对象:你能够用它建立你的模块。例如:
假设这是 rocker.js 文件:
# rocker.js
exports.name = function() { console.log('My name is Lemmy Kilmister'); };
则,在另外一文件中,你能够这样引用:
var rocker = require('./rocker.js');
rocker.name(); // 'My name is Lemmy Kilmister'
强调:
(1)给 exports 赋值实际上是给 module.exports 这个空对象添加了属性,eg:
var name = 'nswbmw'; exports.name = name; exports.sayName = function() { console.log(name); } # 给 exports 赋值实际上是给 module.exports 这个空对象添加了两个属性而已,上面的代码至关于: var name = 'nswbmw'; module.exports.name = name;
module.exports.sayName = function() { console.log(name); }
(2)二者的使用方法不一样,eg:
使用 exports:
# circle.js -- 使用 exports 定义一个方法
exports.area = function(r) { return r * r * Math.PI; }
# app.js -- 使用上面文件里定义的方法
var circle = require('./circle');
console.log(circle.area(4)); # 使用,引用的方式:***.area()
使用 Module.exports:
# area.js -- 使用 module.exports 定义方法
module.exports = function(r) { return r * r * Math.PI;
# app.js -- 使用上面文件定义的方法 var area = require('./area');
console.log(area(4)); # 直接使用,***()
2. Module.exports :
# 修改的 rocker.js
module.exports = 'ROCK IT!'; # 将 module.exports = {}; 变为了 module.exports = ''; exports.name = function() { console.log('My name is Lemmy Kilmister'); };
此时,再次引用执行 rocker.js 以下:
var rocker = require('./rocker.js');
rocker.name(); // TypeError: Object ROCK IT! has no method 'name'
# 发现报错:对象“ROCK IT!”没有name方法
rocker 模块忽略了 exports 收集的 name 方法,返回了一个字符串“ROCK IT!”。由此可知,你的模块并不必定非得返回“实例化对象”。你的模块能够是任何合法的javascript对象--boolean, number, date, JSON, string, function, array等等。
Module.exports
设置任何属性和方法,那么你的模块就是 exports 设置给 Module.exports
的
属性。
eg1: 你的模块是一个类:
# 再次修改 rocker.js
module.exports = function(name, age) { this.name = name; this.age = age; this.about = function() { console.log(this.name +' is '+ this.age +' years old'); }; };
能够这样使用它:
var Rocker = require('./rocker.js');
var r = new Rocker('Ozzy', 62);
r.about(); // Ozzy is 62 years old
eg2: 你的模块是一个数组:
# 再次修改 rocker.js
module.exports = ['Lemmy Kilmister', 'Ozzy Osbourne', 'Ronnie James Dio', 'Steven Tyler', 'Mick Jagger'];
能够这样使用它:
var rocker = require('./rocker.js');
console.log('Rockin in heaven: ' + rocker[2]); //Rockin in heaven: Ronnie James Dio
如今你明白了,若是你想你的模块是一个特定的类型就用 Module.exports,直接 变量/new方法 使用
。若是你想的模块是一个典型的“实例化对象”就用 exports,需引用使用 .***。
module.exports.name = function() { console.log('My name is Lemmy Kilmister'); };
一样, exports 是这样的:
exports.name = function() { console.log('My name is Lemmy Kilmister'); };
强调:这两种定义效果并不相同(本人仍是有点晕,后续将继续学习)。前面已经提到 module.exports 是真正的接口,exports 只不过是它的辅助工具。推荐使用 exports 导出,除非你打算从原来的 “实例化对象” 改变成一个 类型!!
上面也也基本明白了exports和module.exports的关系和区别,但若是同时针对printNextCount()方法存在exports和module.exports,结果如何?
调用结果
从结果能够看出,并无报错,表示能够这么定义,但最终 module.exports 覆盖了exports
****虽然结果不会报错,若是这么用开发中不免会有一些问题存在,因此****
1. 最好别分别定义 module.exports 和 exports
2. NodeJs 开发者建议导出对象用 module.exports, 导出多个方法和变量用 exports
API中还提供了其它的方法,就不细讲了,在上面例子的基础上自已动手一输出就知道了
module.id:返回string类型的模块标识,通常为彻底解析后的文件名
module.filename:返回一个string类型的彻底解析后文件名
module.loaded:返回一个bool类型,表示是否加载完成
module.parent:返回引用该模块的模块
module.children:返回该模块引用的全部模块对象的数组