node. JS provides a simple modular system to allow node. js files to be called each other. Modules are a basic part of the node. JS application, and the files and modules are one by one corresponding. In other words, a node. js file is a module that may be JavaScript code, JSON, or compiled C/s extensions.
node. JS provides exports and require two objects, where exports is the interface that the module exposes, and require is used to obtain an interface from the outside of a module, the exports object of the acquired module.
What interface a module needs to expose externally is determined at the time of definition.
When the module is created, we can use the exports and module.exports two methods to expose the interface. Most of the time, the use of the two is equivalent, but there are subtle differences. The difference is as follows:
exports
is a reference to the point module.exports
;
module.exports
The initial value is an empty object {}
, so the exports
initial value is also{};
require()
is returned module.exports
instead ofexports。
Because exports
it is a pointer to a module.exports
reference, so when we want the module to export an object, exports
and module.exports
both can be used (but exports
also cannot be re-overwritten as a new object), and when we want to export the non-object interface, we must also only overwrite module.exports
.
There are several different ways to define the module definition for node, which is summarized as follows:
Returning a JSON object with the following code is a simple example. Exp.js:
var exp = { "name": "Jeri", "age": +, "email": "[email protected]" = exp;
The method is called: app.js:
var exp = require ("./exp"); Console.log (exp.name);
This method can be used to return some globally shared constants or variables.
For a module, expose several methods. As follows, you can usually return several functions: Exp.js:
var function () { console.log ("func1");}; var function () { console.log ("Func2"== Func2;
The method is called: app.js:
var functions = require ("./exp"); Functions.function1 (); Functions.function2 ();
Returns a constructor, a class example, as follows: Exp.js.js
var function (args) { this.args =function() { console.log ("Myclass.func" ); Console.log (this = MyClass;
Call method is App.js
var MyClass = require ("./myclass"); var New MyClass ("arguments");
Returning a normal function as follows is a simple example. exp.js
var function () { Console.log ("This is a testing function"= func;
The calling method. App.js
var func = require ("./exp"); func ();
Returns an object as follows is a simple example exp.js
var function () { this. SayHello = 'hello 'function() { Console.log (this new MyClass ();
The calling method. App.js
var obj = require ("./myclass"); Obj.func ();
Sometimes we need the module to return a singleton singleton. Can be implemented using the above Method 1 and 4来. That is, the following two kinds of forms: app.js
var Math = { "PI": 3.14, "E": 2.72= MATH;
And
var function () { this. SayHello = "Hello"function() { Console.log ( this new CLASS ();
The above is a summary of several ways to define the module.
node. JS Module Definition Summary