API documentation is boring, the following I collected a number of forums often have some questions and open source code often encountered in the case for you to study.
http://blog.whattoc.com/2013/09/11/nodejs_api_module_2md/
the difference between module.exports and exports
Each node.js execution file automatically creates a module object, and the module object creates a property called exports that initializes a value of {}
Module.exports = {};
Node.js to easily export a functional function, Node.js automatically implements the following statement
Foo.js
EXPORTS.A = function () {
console.log (' a ')
}
Test.js
var x = require ('./foo ');
Console.log (X.A)
See here, I believe we all see the answer, exports is a reference to Module.exports value. When the module.exports is changed, the exports will not be changed, and when the module is exported, the actual export execution is module.exports, not exports
Take another look at the following example
Foo.js
EXPORTS.A = function () {
console.log (' a ')
}
module.exports = {A:2}
Test.js
var x = require ('./foo ');
Console.log (X.A)
Result
2
The exports is invalidated after the module.exports has been altered.
is not beginning a little bit cheerful, the following will be listed in the Open source module, often see several ways to use. module.exports = View
function View (name, options) {
options = Options | | {};
this.name = name;
This.root = Options.root;
var engines = Options.engines;
This.defaultengine = Options.defaultengine;
var ext = This.ext = extname (name);
if (!ext &&!this.defaultengine) throw new Error (' No default engine is specified and no extension was provided. ' );
if (!ext) name = = (ext = This.ext = ('. '!= this.defaultengine[0]? '. ': ') + this.defaultengine);
This.engine = Engines[ext] | | (Engines[ext] = require (Ext.slice (1)). __express);
This.path = this.lookup (name);
}
Module.exports = View;
JavaScript has a word, the function is the object, View is the object, Module.export =view, that is, the equivalent of exporting the entire View object. When the outer module calls it, it can call all the methods of the view. Note, however, that only the static method of the view can be invoked, and the method created by prototype is a private method of view.
Foo.js
function View () {
}
View.prototype.test = function () {
console.log (' Test ')
}
view.test1 = function () {
console.log (' test1 ')
}
module.exports = View
Test.js
var x = require ('./foo ');
Console.log (x) //{[Function:view] test1: [Function]}
console.log (x.test) //undefined
Console.log (X.test1)//[function]
x.test1 () //test1
var app = exports = Module.exports = {};
In fact, when we understand the principle, it is not difficult to understand this kind of writing a bit redundant, in fact, in order to ensure that the initialization of the module environment is clean. At the same time, it is also convenient for us to continue to follow the exports characteristics even after changing the object that Module.exports points to.
Exports = Module.exports = CreateApplication;
/**
* Expose mime.
* *
exports.mime = connect.mime;
example, in which module.exports = CreateApplication changed the Module.exports, let exports failure, through the exports = Module.exports method, let it restore the original characteristics. exports.init= function () {}
The simplest, straightforward way is to export module init. var Mongoose = Module.exports = exports = new Mongoose;
Set of multi-functional, but according to the above described, we should not be able to come up with the answer.