Record the knowledge of Node in the youth training camp
Introduction to the Node. Js
Node.js is a cross-platform Javascrpit running environment based on Google V8 engine
Here are the recommended version management tools
- N: NPM global open source package, is dependent on NPM to install the global, use
- FNM: fast and simple, compatible with.node-version and.nvmrc files
- NVM: standalone package, full Node Version Manager
The characteristics of the Node. Js
Node has three characteristics
Asynchronous I/O
When Node.js performs an I/O operation, the response returns and the operation resumes, rather than blocking the thread and wasting CPU waiting in a loop
Single thread
Advantages: - Don't worry about state synchronization, no ** deadlock ** - No performance overhead due to thread context switching disadvantages: - Can't take advantage of multi-core CPU - errors can cause the entire application to exit, lack of robustness - Too much computing takes up CPU and cannot continue executionCopy the code
cross-platform
Compatible with Windows and (Linux-derived)* NIx platforms, mainly thanks to a layer of platform architecture between the operating system and Node’s upper module system
Modularity mechanism in Node.js
CommnJS specification
Node.js supports the CommonJS module specification and loads modules synchronously
//name.js
const Name = function(){
console.log('my name is yierya')
}
module.exports = {
Name
}
//index.js
const {name} require('./name.js')
name() //my name is yierya
Copy the code
expotts
: can export modules asmodule
A child of the methodrequire
: Introduces other modules into the current module,See more loading methodsmodule
Exports: contains all information about the current module. Methods such as module. Exports__filename
: File path of the current module: absolute path after parsing__dirname
: Path to the folder where the current module is stored
NPM package query principles
// require('lodash') 1. Node_modules 2 in the current directory. If not, node_modules 3 in the parent directory. If not, recurse up the path to node_modules 4 at the root. Will find after loading package. Json main point to the file, if there is no package. The json is a search index. The js, index. Json, index. The nodeCopy the code
Caching mechanisms
Node.js has a caching mechanism, which stores read files in require.cache. The reason for caching is: synchronous loading
- File module search time, if every require needs to traverse the search, poor performance
- In real development, modules may contain side effect code
// cache const mod1 = require('./foo'); const mod2 = require('./foo'); console.log(mod1 === mod2); Function requireUncached(module) {delete require.cache[require.resolve(module)]; return reuire(module); } const mod3 = requireUncached('./foo'); consolo.log(mod1 === mod3); falseCopy the code
Other modular specifications
AMD
Is:RequireJS
In the process of promotion, standardized output, asynchronous loading, highly dependent preloadingCMD
Is:SeaJS
In the process of promotion, standardized output, asynchronous loading, advocating nearby dependenceUMD
Compatible:AMD
andCommonJS
specificationES
: modularity specification at the language level, independent of the environment, can be usedbabel
Compilation, as it is commonly knownES6
In theES
ES Modules
- The ESM is in
ES6
A modular standard proposed at the language level - The ESM mainly has
import
.export
Two key words, noconsole
Print two keywords
ES Modules and CommonJS
-
The CommonJS module prints a copy of the value, the ESM module prints a reference to the value
-
CommonJS modules are loaded at run time, ESM modules are output at compile time (loaded ahead of time)
-
More differences
Can mix but not recommended (import commonjs | | the import of the require)
Some common modules are introduced
Detailed module Introduction
Introduce NPM
NPM is the package manager in Node.js that provides install, delete, and other commands to manage
Typing NPM init will produce a package.json file under the directory
Package. json Configuration information
NPM config set registry= your source path. It is recommended to use the NRM tool to manage NPM sources
Asynchronous programming
callback
promise
Promise is a finite state machine with four states, three of which are core statesPending
.This is a pity.
.My Rejected!
And there is an unstarted stateMore methods in Promise
async
It is a syntactic sugar based on Promise, and adding the async keyword to the function specification tells them to return a Promise instead of returning a value directly. Furthermore, it avoids any potential overhead of synchronization functions to support the use of await.
async function hello() {
return greeting = await Promise.resolve("Hello");
};
hello().then(alert);
Copy the code
event
Node.js has built-in EVETS modules such as Htt Server on(‘ Request ‘) event listeners