1. Introduction to Modularity in node. js
2. Node. JS Core Module
-
URL module
- import module var url = require ("url");
-
Parse () converts the path of a string to an object
var uri = "Http://www.baidu.com:8080/images/1.jpg?version=1.0&time= 1123#ABCD ";
Console.log (Url.parse (URI));
The
-
Format () Converts the Path object to a string
var obj = {protocol: ' http: ', Slashes:true, Auth:null, Host: ' www.baidu.com:8080 ', Port: ' 8080 ', hostname: ' www.baidu.com ', hash: ' #abcd ', search: '? version =1.0&time=1123 ', query: ' version=1.0&time=1123 ', pathname: '/images/1.jpg ', path: '/images/1.jpg?ve Rsion=1.0&time=1123 ', href: ' Http://www.baidu.com:8080/images/1.jpg?version=1.0&time=1123#abcd '}; var str = url.format (obj);
Console.log (str);
3. Where does the core module exist?
- The core module is stored in the Node.exe, and when the Node.exe is running, the core module is loaded, and the require is loaded into memory
- The source code can be found on GitHub, under the Lib folder
- Faster execution of core modules
4. File module (custom module)
Definition File Module Add.js
function add(a,b) { return a + b; } //导出成员 exports.add = add; //module.exports.add = add;
Using the file module main.js
var obj = require("./add.js"); console.log(obj.add(5,6));
5. Package
The
-
COMMONJS Package specification gives programmers the standard for organizing modules, reducing the cost of communication
-
Package usage:
- All modules placed in one folder (package name)
- package put in current item The Node_modules folder in the
- package defines a index.js (file name cannot be changed) export all modules
- Reference package (contract greater than configuration)
-
Import Package Execution process Require ("Calc")
- load Calc as core module, load not successful
- automatically go to node_modules in the current directory to find a package with a file name of Calc
- automatically go to Calc to find in Dex.js Export Module (exported module)
- If index.js error is not found, Package.json config file is required if you want to change the export module
-
Package.js
| name |
function |
| Name |
Package Name |
| Description |
Package Introduction, Introduction of package features |
| Version |
Version number, for version control |
| Keywords |
Keyword array for searching in NPM |
| Main |
Require first check this field when introducing a package |
| Dependencies |
Mark the list of packages that the current package depends on, and NPM will automatically load the dependent packages |
| Author |
Package author |
| License |
Open Source Licensing |
{ "name": "calcpack", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { //可以通过npm run来执行 "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC"}
name | function |---|---| Package.js | Package description File Bin | Store executable file lib | store JavaScript code DOC | store Document Test | Store Unit test Case Code README.MD | Documentation describing the role and usage of the package
- Standard package execution Procedures
- Loading Calcpack as a core module, loading is unsuccessful
- Automatically go to the Node_modules in the current directory to find a package with the file name Calcpack
- If there is package.json in Calcpack, and the value of the main property is specified, the. JS module specified by main is loaded first (export module)
- If there is no Package.json, or the main attribute is not specified, automatically go to Calcpack to find the Index.js exit module (exported module)
- If the index.js error is not found
6. Release the package
Install package
- Installing from the Network
- Current directory install NPM install package name
- Global Install NPM Install package name-G
- Path to locally installed NPM install package
- Uninstall Package NPM Uninstall package name
require () Load rule
- Load module or package from cache first
- Load the file module to use a relative path. /
- The file module can be loaded without the suffix name if the suffix name is not written in the order of. js >. node >. JSON
- Loading the JSON file, it is recommended to write the suffix. JSON
- Load core modules or packages, do not write paths and suffixes
- Module.paths loading the node_modules, load it in the order of this array
Modularity of the Nodejs