node.js module preliminary understanding

https://www.cnblogs.com/dolphinX/p/3485260.html

 

 In the development of a complex application, we need to split the various functions of the package to a different file, referencing the file when needed. No one will write a file tens of thousands of lines of code, so that the readability, maintainability and reusability are very poor, almost all programming languages ​​have their own organization modules, such as Java package, C # in assemblies, etc., node.js modules and packages use to organize, and the mechanism to achieve a CommonJS reference standard, although not in full compliance, but the gap, very simple to use.

What is a module

In node.js modules and files is one to one, that is to say a node.js file is a module, the document may be our packaged some JavaScript method, JSON data, compiled C / C ++ development and other in misunderstanding about node.js is mentioned node.js framework

Wherein http, fs, net node.js other core modules are provided, using the C / C ++ implementation, the outer package in JavaScript.

Create, load module

The concept of modules in node.js is very simple, take a look at how to create a module for the development of our own multiplex.

Create a very simple module, a module in a file that is in node.js, so we create a test.js file creates a module

test.js

Copy the code
var name='';

function setName(n){
    name=n;
} 

function printName(){
    console.log(name);
}
Copy the code

 The question is how to make external access to this module, we know that the use of client-side JavaScript script tag introduced JavaScript file you can access its content, but this brought a lot of drawbacks, the biggest is the same scope, resulting in conflict, so that front-end Masters We have come up with other ways to execute the function immediately, the use of closures to solve. node.js exports and require the use of objects to solve the problem of external interfaces and provide a reference module.

We can hope that the module is defined as the content of external access to the exports object, for test.js slight modifications on it

test.js

Copy the code
var name='';

function setName(n){
    name=n;
} 

function printName(){
    console.log(name);
}

exports.setName=setName;
exports.printName=printName;
Copy the code

In this way we create index.js in the same path, use require cite test.js module

index.js

var test=require('./test');

test.setName('Byron');
test.printName();

exports an object

Sometimes we want to make an object module is provided externally, modify test.js

test.js

Copy the code
var Student=function(){
    var name='';

     this.setName=function(n){
        name=n;
    }; 

    this.printName=function(){
        console.log(name)    ;
    };
};

exports.Student=Student;
Copy the code

 这样我们对外提供了一个Student类,在使用的时候需要这样

var Student=require('./test').Student;
var student=new Student();
student.setName('Byron');
student.printName();

 require('./test').Student 很丑陋的样子,我们可以简单修改一下exports方式,使require优雅一些

test.js

Copy the code
var Student=function(){
    var name='';

     this.setName=function(n){
        name=n;
    }; 

    this.printName=function(){
        console.log(name)    ;
    };
};

module.exports=Student;
Copy the code

 这样我们的require语句就可以优雅一些了

var Student=require('./test');

 

很神奇的样子,不是说好的exports是模块公开的接口嘛,那么module.exports是什么东西?

module.exports与exports

事实的情况是酱紫的,其实module.exports才是模块公开的接口,每个模块都会自动创建一个module对象,对象有一个modules的属性,初始值是个空对象{},module的公开接口就是这个属性——module.exports。既然如此那和exports对象有毛线关系啊!为什么我们也可以通过exports对象来公开接口呢?

为了方便,模块中会有一个exports对象,和module.exports指向同一个变量,所以我们修改exports对象的时候也会修改module.exports对象,这样我们就明白网上盛传的module.exports对象不为空的时候exports对象就自动忽略是怎么回事儿了,因为module.exports通过赋值方式已经和exports对象指向的变量不同了,exports对象怎么改和module.exports对象没关系了。

大概就是这么过程

Copy the code
module.exports=exports={};
......

module.exports=new Object();

exports=xxx;//和new Object没有关系了,最后返回module.exports,所以改动都无效了
Copy the code

一次加载

无论调用多少次require,对于同一模块node.js只会加载一次,引用多次获取的仍是相同的实例,看个例子

test.js

Copy the code
var name='';

function setName(n){
    name=n;
} 

function printName(){
    console.log(name);
}

exports.setName=setName;
exports.printName=printName;
Copy the code

 

index.js

var test1=require('./test'),
    test2=require('./test');

test1.setName('Byron');
test2.printName();

 

 

执行结果并不是'',而是输出了test1设置的名字,虽然引用两次,但是获取的是一个实例

require搜索module方式

node.js中模块有两种类型:核心模块和文件模块,核心模块直接使用名称获取,比如最长用的http模块

var http=require('http');

 

In the example above, we use a relative path './test' to get the custom file module, so there are several search node.js load modules way?

  1. The core module with the highest priority, use a name loaded, when there are naming conflicts loaded first core module
  2. File module can only follow the path load (can be omitted, the default name .js expand, not then need to show written statement)
    1. Absolute path
    2. relative path
  3. Find node_modules directory, we know will create node_module directory (if not present) to install the module in the current directory when calling npm install <name> command when neither require encountered a core module, the module path is not represented in the form when the name of the current attempts to node_modules directory to find the directory is not such a module. If none is found, it will continue to look in the parent directory of the current in the node_modules directory, this process is repeated until it encounters the root of so far.

Guess you like

Origin www.cnblogs.com/alps/p/11353705.html