1. Example of a reference to a module
var math = require (' math ');
In the Common.js specification, there is the require () method, which accepts the module identifier, which introduces the API of a module into the current context.
2. Module definition
In the module, the context provides the require () method to introduce an external module. Corresponding to the introduced functionality, the context provides a method or variable for the exports object to be used everywhere in the current module, and it is the only exit anywhere, in the module, there is also a Module object, which represents the modules themselves, and exports is the attribute of the modular. In node, a file is a module in which the method is mounted on the exports object as a zodiac to define the way it is exported:
Math.js
Exports.add = function () {
var sum = 0,
i = 0,
args = arguments,
L = args.length
while (I < 1) {
Sum + = Args[i + +];
};
return sum;
};
In another file, after we introduce the module through the Require () method, we can invoke the defined property or method:,
Program.js
var math = require (' math ');
Exports.increment = function (val) {
Return Math.add (Val, 1);
};
node. JS Module Reference