What is a node
Node is not a language, but is based on ChromeV8 engine runtime (runtime). It is an environment for JS to run in the back end, and does not include the complete JS set
Why use Node
We often hear that Node has the following advantages:
- Naturally excellent high concurrency capability
- Non-blocking (asynchronous, callback)
- Strong I/O processing capability
- Modularity (CommonJs)
Understand the following keywords:
- What is synchronous asynchronism
- What is asynchronous I/O
- What is blocking and non-blocking
- What are event loops and event-driven
- What is single threading
- What is a process
- What is a child process (extension: Start a child process, process communication)
Preliminary understanding of the process
Principles of browsers
- Browsers are multi-process
- Processes have multiple threads, such as the UI thread in the figure above
- The UI thread and JS thread are mutually exclusive
- The browser rendering engine is also called the browser kernel
- Chrome plugin corresponds to a process (third-party plugin process)
- GPU Improves rendering efficiency, which corresponds to the GPU process
I/O intensive (vs. JAVA)
Figure 1: Tomcat multithreading web tasks
Figure 2: Node single-threaded Web tasks
See node in the Web application I/O (read and write operations, input and output, request and response) operations are very efficient. Node is suitable for I/O intensive applications, not CPU intensive ones
Node develops three major components
- NPM package Manager (everyone knows)
- NVM Node version management tool
- NRM node NPM source management tool
The global variable
- console
- Process is often used to set environment variables
- Buffer
- …
process
console.log(process);
Copy the code
MAC set NODE_ENV
> $ export NODE_ENV=dev
Copy the code
Modularity of Node
Each node file is a module, a partition rule of node modularity
- The problem
- In the node file
console.log(this)
What is the output? - If it’s not
var
Declare a variablea
So thisa
Where will it be?
- In the node file
- A couple of caveats
- Asynchronous microtasks,
process.nextTick()
In thethis
setTimeout()
与setImmediate()
In thethis
- talk is cheap, show me the code
- Asynchronous microtasks,
The arrow function this points to the parent scope of the function, so use () => {} whenever possible in callback functions.
setTimeout((a)= > {
console.log(this);
// console.log(arguments);
}, 0)
Copy the code
From the arguments example above, we can see the parent scope of the arrow function, i.e. the current Node module has arguments. Does this prove that the Node module is essentially a closure? (It’s not that esoteric.)
// try this
console.log(a);
Copy the code
So node modularizes, a module is passed in
- __dirname
- __filename
- exports
- module
- require()
What are the benefits of modularity
- High cohesion
- Low coupling
- Easy to maintain
Node relies on the CommonJS specification
- A file is a module
- use
require
To refer to,require()
Can write code logic, no top - use
exports
/module.exports
Extension:
- Modularity in browsers
- CMD: SeaJS nearby dependency
- AMD: Requirejs pre-dependency
- ES6 standard module
Take a closer look at node modularity with some examples
First example: The Node module has no output
/ / a file
const a = 1;
const b = function() { console.log('this is file a'); }
B / / file
const fileA = require('./a.js');
console.log(fileA);
Copy the code
The real implementation of the Node module is:
(function(exports, require, module, __filename, __dirname) {
module.exports = exports = this = {};
/*------ our code -------*/
return module.exports; }) ()Copy the code
Now that we know how node module input and output work, let’s look at the following example
/ / a file
exports = 123;
B / / file
const fileA = require('./a');
console.log(fileA);
Copy the code
Here’s another question:
We’ve seen the modular nature of Node above, and from our coding experience we know that require() is executed immediately
/ / a document
console.log('Here's file A');
const func = (a)= > {
console.log('This is a function of file A');
}
module.exports = func;
/ / b file
const fileA = require('./a');
fileA();
Copy the code
Then look at the following question:
/ / a document
console.log('Here's file A');
const func = (a)= > {
console.log('This is a function of file A');
}
module.exports = func;
/ / b file
require('./a');
require('./a');
require('./a');
Copy the code
Articles in the series will be updated regularly, and previous articles will be optimized regularly