Node.js's module mechanism

Original article in Public number: ape Zhou Xiansen program. This platform is not updated regularly, like my article, I welcome attention to the micro-channel public number.
file

In fact, in the development of JavaScript, which is mainly applied widely in the browser front end. Because in practical application, JavaScript API's performance depends primarily on the ability level of support the host environment, and in most early, only to BOM, DOM support, with the advent of HTML5, there are more and more powerful in the browser API for JavaScript calls, but these occurred in the front, back JavaScript norms are lagging far behind. Java has a class file, Phthon have import mechanism, PHP has include and require, but the way the introduction of JavaScript code by script tags appear disorganized, our post-maintenance more difficult. For JavaScript, there are still four major drawbacks:

  •     1.没有模块系统。
  •     2.标准库比较少。
  •     3.没有标准,统一的接口。
  •     4.缺乏包管理系统。

Node.js implements a very easy to use modular systems, and Node package management system NPM good support for the package specification makes it more effective in Node application development process. This article, aimed and achieved Node module package will be explained.

Node module specification

In fact, the definition module is very simple, divided into module reference module module definition and identification of three parts.

1) module reference

Presence require () method Node.js, this method accepts identification module, in order to introduce a module API present context.

2) Module Definition

Since we can use require () to introduce modules that can naturally lead to the module. Node.js provides methods and variables used to derive the current module exports the object, and exports the only outlet is derived. In each module, there is a module object that represents itself, exports in fact a property module module. In the Node.js, a file is actually a module, we will need to export the methods and properties as the binding properties can be exported in the method or property on exports object.

In yet another module, the module can be introduced through the require (), can be derived using the method of sum ().

3) identification module

In fact, the identification module is passed to require () method of the parameters, the module identifier string must be consistent camelCase ./,../ or in the beginning of the path, intake module identification module may be omitted .js suffix.

The advantage of the module is specific methods and variables defined in a specific scope, so that developers do not have to consider the problem of contamination of the variable.

Node.js modules to achieve

In Node.js, there are three types of modules, one of which is the core module provides Node.js, like on previous said fs file module, database module database, there is a category of developers to write their own file module, it is just an example of such test.js module, the third category is custom module, which is a special file module, generally in the form of a file or a package, for example into the desired package mysql jar.

The introduction of modules in Node.js, we need to go through three steps:

(1) Path Analysis

For file module, the module is introduced exact file identifies the specified position, the path analysis may be omitted in a lot of time, after core module loading speed.

Custom modules one by one is comparing the path from the root directory of the project until it finds the target module so far. Therefore, custom module paths deeper, more time-consuming path analysis, so self-defined module loading speed is the slowest.

(2) document localization

Have actually said, the identification module may not include the suffix, so Node.js when the file is positioned in turn supplement .js, .json, .node extension, and then to locate files, because Node.js is single-threaded, so jam occurs when the file positioning, so if the introduction of the module suffix is ​​.json or .node, you can add a suffix when introduced, can improve search speed.

(3) compile and execute

After you define the specific file, Node.js creates a module object, and then introduced into the module and the compiler. Each module is a compilation of its success as an index file path will be cached on the cache object, introduced in order to improve the performance of the secondary module.

Node.js core module during compilation of the source code, compiled into a binary file directly, then loaded directly into memory, so that when introduced into the core module, and the file locator compiler implementation can skip two steps, and the core priority determination module is in the path analysis, the core module loading speed is the fastest.

File module is dynamically loaded at runtime, the path analysis, and compiling the file positioning these three steps are performed can not be omitted, so the load slowly than the core module.

Node.js through the introduction of the module cached to reduce secondary module performance overhead introduced secondary load modules all use cache priority mode. Check the cache files in preference to the core module of the module.

NPM package management tool

Node module just said, but although we can reference module, but between the module and the module is still hashing in the country, and can not directly reference each other. The Node package management tool NPM module will be linked to each other. In fact, the package is further organized JavaScript code is based on a modular basis.

In fact, NPM will have a package description file package.json, generally located in the root directory of the package, all acts NPM are closely linked with the package file description. Several have talked about in front of NPM as the default package management tools will be installed as Node environment together.

NPM common functions

NPM help Node completed the release of third-party modules, installation and dependence. Because of the existence of the NPM, the formation of a good ecosystem between Node and third-party modules, and gradually more and more powerful. Then roughly on the next few NPM common commands

  • npm --version view the current version of NPM

  • View npm help explain

  • npm help command to view specific instructions

  • Execute command opens the corresponding documentation for the command in the browser

  • npm install installation dependencies, --save default parameters, i.e. implicitly added to the package.json

  • Execute the command, NPM creates node_modules directory in the current directory, and then create the corresponding dependencies node_modules directory, and then rely package to extract the directory.

  • npm init generated in this directory initialization file package.json

  • asl uninstall Unloading dependencies, --save default parameters, i.e., removed from the package.json

  • npm ls view the current dependencies directory

  • npm root -g view the global installation address

  • asl letter View the current version dependent

NPM existing problems

In the NPM platform, everyone can share packages, the package is no way to guarantee quality, and Node.js runs on the server, we need to consider security issues. So a good modules need to meet several modules:

  1. Have a good test

  2. Good documentation

  3. Good test coverage

  4. Code specifications have good

Today content on here, in fact, this article does not involve writing code, but from the perspective of the module to understand Node.js, Node.js modules by specification, to make up for JavaSCript no structural deficiencies, and by NPM unified management package that depend on project development issues are effectively addressed.

Next asynchronous programming from the perspective that we continue to understand with Node.js, the next good-bye!
No. I personally welcome attention to the public: Program ape Zhou Xiansen
file

Guess you like

Origin www.cnblogs.com/niyueling/p/11562990.html