There is no meaning in life without suffering, reading for the great rejuvenation of the Chinese nation.

Antecedents to review

The applets interface getUserInfo will be replaced by getUserProfile on 2021-04-13, so you need to consider whether the project needs to be updated.

Today is a review of the commonJS specification and nodeJS module mechanism, which will be picked up in the next article if I don’t finish this one.

Background on specification and module mechanisms

Javascript has long been thought of as a web script. The web1.0 era was mostly about form validation or web effects. In the era of web2.0, it has gradually been taken seriously and roughly experienced the changes of tool class library (jquey for compatible browser)- > component (development function module)- > framework (function module sub-organization)- > application (business module organization).

In this process of change, JS continues to be abstract, aggregation. Compared with using Script tag to organize code, abstract JS can better organize business logic, especially after the emergence of Node, JS front-end application is more active, and the community has slowly customized the corresponding specification for Javascript.

Vision of the Commonjs specification

One starting point for the Commonjs specification is that you want javascript to run anywhere.

In practice, the working scene of Javascript is basically limited to the API support level in the host browser. Although many new and powerful apis appear in browsers after Web2.0 and HTML5 emerge, compared with other languages, the specification of Javascript itself is still very weak.

  • No modular system.
  • The standard library is less.
  • No standard interface.
  • Lack of a package management system.

The Commonjs specification points the way to developing large applications for JavaScript, including modules, binaries, buffers, character sets, I/O streams, file systems, sockets…

Use of the Commonjs specification

Commonjs specification is very simple to use, including module reference, module definition, module identifier three parts.

  • The module reference. Sample code:
// Introduce the module API with the require method

var fs =  require('fs')



Copy the code
  • The module definition. One exists in the modulemoduleObject, representing the module itself, and the context provides oneexportsObject is used to export methods or variables of the current module,And it's the only exit that comes out. At the same time,exportsismoduleProperties. inNodeA file is a module. Sample code:
// saveToken.js

const fs =  require('fs')



module.exports = function (filename,readStream){

    return new Promise(resolve= >{

        const writeStream = fs.createWriteStream(filename);

        writeStream.on('finish', () = > {

            setTimeout(resolve,100);

        })

        readStream.pipe(writeStream)

    })

}



Copy the code
  • Module identifier. The module id is actually passed torequire()The argument to the method must be a qualified string named small camel, or a string named...The initial relative path or absolute path can be ignoredjs.

In this way, each module has its own space and does not interfere with each other, and the whole business logic is clear.

The Node module,

In Node, modules can be divided into two categories. One is that the modules provided by Node become core modules. The other is user-written modules called file modules.

Introducing modules into Node generally goes through the following steps:

  • Path analysis
  • File location
  • Compile implementation

The next article will explain these processes

conclusion

  • CommonJs specificationbackground
  • CommonJs specificationWhat problems can be solved
  • CommonJs specificationSimple to use

One last word

  1. Move your rich little hands and “like it.”
  2. Move your rich little hands, “click here”
  3. All see here, might as well “add a follow” wechat search public number “javascript advanced Programming”
  4. Might as well “forward”, good things to remember to share
Summary of javascript basic knowledge