Nodejs study notes-modular

Node.js adopts the CommonJs specification. In NodeJS, the code is generally divided into different JS files reasonably. Each file is a module, and the file path is the module name.

When writing each module, there are three pre-defined variables of require, exports, and module available for use

Classification of modules in Node.js:

  • Core module (built-in module already encapsulated)
  • Self-defined module
  • Third-party modules (downloaded by npm)

require

The require function is used to introduce another module in one module

Pass in a module name and return a module export object

Usage: let cc = require("module name"), where the module name can be an absolute path or a relative path, and the suffix of the module .js can be omitted

	let cc1 = require('./main.js')
	let cc2 = require('home/src/main.js')
	let cc3 = require('./main')

The require() function has two functions:

  • Execute the code in the imported module;
  • Return the interface object in the imported module;

exports

The exports object is used to export the public methods or properties of the current module

When other modules use the current module through the require function, they get the exports object of the current module.

Usage: exports.name, name is the name of the exported object

	exports.add = function () {
    
    
		let i = 0
		console.log(++i)
	}
​
	导出一个add方法供其他模块使用

module.exports

module.exports is used to export a default object without specifying the object name

It is common to modify the original export object of the module. For example, the original module exported an object, we can modify it to export a function through module.exports

	module.exports = function () {
    
    
	  console.log('hello world!')
	}

The difference between module.exports and exports

  • Each module in Node has a module object, and there is an exports attribute in the module object as an interface object. We need to mount the public methods or attributes between modules in this interface object, so that other modules can use these public Methods or properties.
  • At the end of each module in Node, it will return: module.exports.
  • Each module in Node assigns the object pointed to by module.exports to a variable exports, that is, exports = module.exports.
  • module.exports = XXX, which means that the current module exports a single member, and the result is XXX.
  • If you need to export multiple members, you must use exports.add = XXX; exports.foo = XXX; or use module.exports.add = XXX; module.export.foo = XXX;.

Module initialization

The JS code in a module is executed only once when the module is used for the first time, and it is initialized once during execution

Cache it for subsequent use

Main module

The module passed to NodeJS via command line parameters to start the program is called the main module

The main module is responsible for scheduling other modules that make up the entire program to complete the work. For example, when the program is started by the following command, main.js is the main module

	$ node main.js // 运行main.js启动程序,main.js称为主模块

Instance

index1.js:

	function fn(){
    
    
	    console.log(123);
	}
	
	exports.fn = fn;

index2.js:

	let cc1 = require('./index1.js')
	
	console.log(cc1);
	cc1.fn();

Insert picture description here

Guess you like

Origin blog.csdn.net/Nozomi0609/article/details/109474050