Node. js creation Module

Source: Internet
Author: User

A module is a basic component of a Node. js application. Files and modules correspond to each other. In other words, a Node. js file is a module, which may be JavaScript code, JSON, or compiled C/C ++ extensions. Var http = require ('HTTP '). http is a core module of Node. js, which is implemented in C ++ internally and encapsulated in JavaScript externally. We can obtain this module through the require function before using the objects in the module.

1. Create a module

Creating a module is very simple, just like writing a single function. However, after creating a module, you need to call it elsewhere to make full use of it, just like a function. First, create a module. js to create a module:


[Javascript]
Var x, y;
Exports. setArgs = function (a, B ){
X =;
Y = B;
}
Exports. mult = function (){
Console. log (the product of x + "and" + y + "is:" + x * y );
}

Var x, y;
Exports. setArgs = function (a, B ){
X =;
Y = B;
}
Exports. mult = function (){
Console. log (the product of x + "and" + y + "is:" + x * y );
} Then write a getModule. js to call the content of this module:


[Javascript]
Var module = require ("./module ");
Module. setArgs (5, 4 );
Module. mult ();

Var module = require ("./module ");
Module. setArgs (5, 4 );
Module. mult (); In websotrm, right-click and run getModule. js. The console outputs the result.


Of course, this is just the simplest example, simply learning how to create and call modules.

 

2. Overwrite exports

The code above demonstrates how to encapsulate a function into a module, and then demonstrates how to encapsulate an object into a module. Modify module. js as follows:


[Html]
Function calc (){
Var x, y;
This. setArgs = function (a, B ){
X =;
Y = B;
Console. log ("the parameter is set successfully. ");
};
This. mult = function (){
Console. log (the product of x + "and" + y + "is:" + x * y );
};
This. add = function (){
Console. log (the sum of x + "and" + y + "is:" + (x + y ));
};
}
Exports. calculator = calc;

Function calc (){
Var x, y;
This. setArgs = function (a, B ){
X =;
Y = B;
Console. log ("the parameter is set successfully. ");
};
This. mult = function (){
Console. log (the product of x + "and" + y + "is:" + x * y );
};
This. add = function (){
Console. log (the sum of x + "and" + y + "is:" + (x + y ));
};
}
Exports. calculator = calc; the Code above encapsulates a calc function into the module and exports it as a calculator object. You can call related Algorithm functions by instantiating This calculator object. Modify getModule. js as follows:


[Javascript]
/Obtain the calculator object
Var module = require ("./module"). calculator;
Console. log (module); // check what it is.
// Instantiation. If this step is not performed, an error occurs if you want to directly use module. Func.
Var moduleEntity = new module ();
// Verify whether moduleEntity can call the function normally
Console. log (moduleEntity. setArgs );
// Verification passed. This method can be used
ModuleEntity. setArgs (4, 2 );
ModuleEntity. add ();
ModuleEntity. mult ();

// Obtain the calculator object
Var module = require ("./module"). calculator;
Console. log (module); // check what it is.
// Instantiation. If this step is not performed, an error occurs if you want to directly use module. Func.
Var moduleEntity = new module ();
// Verify whether moduleEntity can call the function normally
Console. log (moduleEntity. setArgs );
// Verification passed. This method can be used
ModuleEntity. setArgs (4, 2 );
ModuleEntity. add ();
ModuleEntity. mult ();

 

However, this method can be slightly changed. The export time code is changed:

[Javascript]
Module. exports = calc; // export calc directly to module

Module. exports = calc; // directly export calc to the module call method and modify it as follows:


[Javascript]
Var Calculator = require ("./module ");
Calculator = new Calculator ();
Calculator. setArgs (7,3 );
Calculator. add ();

Var Calculator = require ("./module ");
Calculator = new Calculator ();
Calculator. setArgs (7,3 );
Calculator. add (); I personally think the two are the same, that is, a little less code.
The only change in the module interface is that module. exports = calc replaces exports. calculator = calc. When this module is referenced externally, its interface object is the calc object to be output, rather than the original exports. In fact, exports itself is only a common null object, namely {}, which is used to declare an interface. In essence, it creates a limited access interface for the interior of the module closure. Because it does not have any special place, it can be replaced by other things, such as the calculator object in the above example.
Note: you cannot assign values to module. exports directly by assigning values to exports. Exports is actually just a module. exports points to the variable of the same object. It is released after the execution of the module, but the module does not. Therefore, you can only specify the module. exports to change the access interface.

 

/* For more information, see Node. js Development Guide */

/* All the above Code is developed through webstorm */

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.