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

  1. N: NPM global open source package, is dependent on NPM to install the global, use
  2. FNM: fast and simple, compatible with.node-version and.nvmrc files
  3. 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 asmoduleA child of the method
  • require: Introduces other modules into the current module,See more loading methods
  • module 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

  1. File module search time, if every require needs to traverse the search, poor performance
  2. 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

  • AMDIs:RequireJSIn the process of promotion, standardized output, asynchronous loading, highly dependent preloading
  • CMDIs:SeaJSIn the process of promotion, standardized output, asynchronous loading, advocating nearby dependence
  • UMDCompatible:AMDandCommonJSspecification
  • ES: modularity specification at the language level, independent of the environment, can be usedbabelCompilation, as it is commonly knownES6In theES

ES Modules

  • The ESM is inES6A modular standard proposed at the language level
  • The ESM mainly hasimport.exportTwo key words, noconsolePrint 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