node.js 16 module system

Refer to the original article - http://bjbsair.com/2020-03-22/tech-info/2817/

const http = require('http')

In node.js, an application program consists of several modules, and these modules, we can be understood as a JavaScript file, ending in .js files.

Native module

For the above-mentioned 'http' native module, after the system comes node.js installation. By using only modules require for introduction to.

In this code, used to refer to http require function module, the function returns http module, the object is assigned to the http i.e. after the properties and methods can be accessed by the module http http.

const http = require('http')

http.createServer(function (request, response) {

// callback function content

}

Also known as block core module, the core module used as follows:

  • assert assert processing
  • Buffer memory for buffering data conversion
  • Creation and management of child processes child_process
  • cluster multiprocessing
  • console console
  • Built-in debugger debugger
  • dns DNS-related
  • events Event Processing
  • http HTTP server and client
  • https HTTPS server and client
  • net TCP server and client
  • os operating system information
  • path file path release
  • querystrmg Xun string search processing used in the HTTP request
  • repl achieve REPL (Read-Eval-Print-Loop)
  • zlib compression and decompression is achieved by processing the data library zlib

Introducing custom module

In practice, using only the native module is not enough, we need to use other modules define their own team, which is .js file. So how to use?

With the use of native modules, but also through the require function, except that the need to write a file path name of the module.

const circle = require('./circle.js');

circle.js files in the example is a custom module file. Practical work, often established node_modules directory in the root directory, a directory corresponding to the different modules in accordance with established business rules.

Export module exports

A module can only be used variables and methods of the current module. If you want to be used by other modules, need to be derived by the module exports objects, methods, attributes .

Next we look at the contents of circle.js

const PI = 3.14;  
exports.area = (r) => PI * r ** 2;  
exports.circumference = (r) => 2 * PI * r;  
exports.PI = PI;

In the example we can see that this is a calculation block area and perimeter of circle. Use the exports in the module derived

  • Two methods , area (r) and the circumference (r)
  • A property , PI

So back to our original code, since we introduced circle.js, only need to write the following code can be called circle.area (r) this method.

const circle = require('./circle.js');  
console.log(`半径为4的园面积:  ${circle.area(4)}`);  
console.log(`PI =  ${circle.PI}`);

After you save the source file as appCircle.js, run appCircle.js following results

D:\Projects\nodejs\NodeDemo>node appCircle.js

Park area of ​​a radius of 4: 50.24

PI = 3.14

The module is defined and derived class

The example above we see the properties and methods of exporting module. Well, from an object-oriented point of view, if we can define a class module and export. The following is an example stored in square.js in.

module.exports = class Square {  
  constructor(width) {  
    this.width = width;  
  }  

  area() {  
    return this.width ** 2;  
  }  
};

Example, can be used directly "module.exports" class is derived. Derived class called Square, are used to calculate the number of squares. Construction of such width by constructing, using the method of Area (), returns the number of squares.

When the call we just need to introduce the class, then call area (method can).

const Square = require('./square.js'); //引入类  
const mySquare = new Square(2); //构建Square类  
console.log(`平方数: ${mySquare.area()}`); //调用类方法  

Output:

D:\Projects\nodejs\NodeDemo>node appSquare.js

Square numbers: 4

Introduction node.js modules on here.

Guess you like

Origin blog.51cto.com/14744108/2481522