Node Learning Notes

1. Common node attributes

console.log(global);// Globally accessible attributes, (global variables)
console.log(__dirname); // The directory in which the current file is executed is dead (absolute path)
console.log(__filename); // The absolute path to the file itself
console.log(global.process);// Process represents a process and can obtain some runtime environment and parameters
//process
//(1), platform win32=> Window MAC => Darwin
//(2), CWD current working directory can be changed webpack will automatically find the directory where webpack is running to find webpack.config.js
// env reads global environment variables by default when executing code
//(4) argv is the argument passed when executing the code
console.log(process.platform)
/ / env file
if (process.env.NODE_ENV === 'development') {
    console.log('dev');
} else {
    console.log('prod');
}
// [Exe file where node is executed, current executing file,.. other parameters]
console.log(process.argv); // The Commander plugin will parse the user commands itself. It will parse the corresponding functions based on the parameters passed by the user to develop a scaffold, which will be used by the running tool
// process.argv[0]; // Node executable file
// process.argv[1]; // Which file is executed by Node
Copy the code

2. Event loop and nextTick in Node

Node eventLoop: the value of the browser and node’s eventLoop, executed on node10, is different in nature

Differences: Browsers have one macro task and one microtask, while Node has multiple macro task queues

The default is to execute code from top to bottom, flush callback methods on each queue in turn, flush microtasks with each invocation of a macro task,

Timers: Stores all timer callbacks [FN,fn,fn],

Poll: Stores asynchronous IO operations. In Node, almost all asynchronous API callbacks are processed at this stage

Check: Stores all setImmidiate callbacks

This phase of execution has beensetTimeout() andsetIntervalThe scheduling callback function of (). ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ > │ timers │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ | execution delays to the next loop iteration of I/O callback. │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ pending callbacks │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ | system for internal use only. │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ idle, prepare │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ | retrieve a new I/O events; Perform the I/O related callback ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ incoming: │ │ │ poll │ < ─ ─ ─ ─ ─ ┤ connections, │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ data, etc. │ │ setImmediate () callback function is executed here. └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ check │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ | some closed │ a callback function ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ └ ─ ─ ┤ close callbacks │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘Copy the code

1, the main stack

2, detection time has not arrived at the time, there is on the empty

3. Poll Delete the IO operations one by one

Check if there is any content in setImmidiate. If there is any content, clear the check phase. If there is no content, block

5, do not stop to see whether the timer has reached the time, if there is, then go back to continue to execute if all executed, then shut down

// The implementation of the nextTick node does not belong to the EventLoop in the node and has a higher priority than the promise
setTimeout(() = >{
    console.log(1);
},0);
Promise.resolve().then(() = >{ 
    console.log(2);
});
process.nextTick(() = >{// Priority over microtasks
    console.log(3);
});
console.log(4)
/ / 4,3,2,1
Copy the code

3. Module specification in Node

Es6Module CommonJS for node modules

Es6Module – Tree-shaking CommonJS modules packaged with WebPack

Es6Module is a “static” module that supports tree-shaking and can be analyzed at compile time. Commonjs is a “dynamic” module that can bring in modules at code execution (tree-shaking is not possible). So our VUE module is generally introduced with es6Module specification

4. Built-in modules are commonly used in Node

Common: fs path VM

const fs = require('fs');
const path = require('path');
const vm  =require('vm');
let r = fs.readFileSync('./a.js'.'utf8')// Read the file synchronously, return a string
fs.existsSync('./a.js')// If file suffixes exist, return a Boolean value
path.extname()// Get the file name extension
console.log(path.resolve(__dirname, 'a'.'b'.'c')); Process.cwd () is used by default. If there is a path /, it will return to the root directory
console.log(path.join(__dirname,'a'.'b'.'c')); // Just concatenation, will not produce absolute path, encountered/will also be put together
console.log(path.relative('a/b/c/1.js'.'a')); // Get the relative path based on the path
console.log(path.dirname('a/b/c')); // Take the parent path of the current file
vm.runInThisContext(`console.log(a)`);// Turn a string into js to perform something like eval, which is affected by the execution environment
Copy the code

5, code implementation require

First create the a.js and a.json files a.js

var a =100;
console.log(this= = =module.exports)
module.exports = a
Copy the code

a.json

{
	"name":"demo"
}
Copy the code

req.js

const fs = require('fs');
const path = require('path');
const vm = require('vm');
class Module{
    constructor(id){
        this.id = id
        this.exports={}
    }
    load(){
        let ext = path.extname(this.id); // Get the file name extension
        Module._extensions[ext](this);
    }
}
Module._cache = {}
Module._extensions = {
    '.js'(module) {let script = fs.readFileSync(module.id,'utf8');// Read files synchronously
        let templateFn = `(function(exports,module,require,__dirname,__filename){${script}}) `;
        let fn = vm.runInThisContext(templateFn);// Run context, run template string
        // let fn1 = (function(exports,module,require,__dirname,__filename){
        // eval(script)
        / /})
        let exports = module.exports;
        let thisValue = exports; // this = module.exports = exports;
        let filename = module.id;
        let dirname = path.dirname(filename);// Take the parent path of the current file
        // The function call is used to 1. Change this to 2
        fn.call(thisValue,exports.module,req,dirname,filename); Exports = 0; // exports = 0; // exports = 0;
    },
    '.json'(module) {let script = fs.readFileSync(module.id,'utf8');
        module.exports = JSON.parse(script)
    }
}
Module._resolveFilename = function(id){
    let filePath = path.resolve(__dirname,id)
    let isExists = fs.existsSync(filePath);// If file suffixes exist, return a Boolean value
    if(isExists) return filePath;// If so, return directly
    // If not, the for loop tries to add the suffix
    let keys = Object.keys(Module._extensions); // From now on new methods of Object will be added to Reflect
    for(let i =0; i < keys.length; i++){let newPath = filePath + keys[i];
       if(fs.existsSync(newPath)) return newPath
    }
    throw new Error('module not found')}function req(filename){
    filename = Module._resolveFilename(filename); // 1. Create an absolute reference address for subsequent reading
    let cacheModule = Module._cache[filename]
    if(cacheModule) return cacheModule.exports; // Just drop the last cached module to you
    const module = new Module(filename); // 2. Create a module based on the path
    console.log(module)
    Module._cache[filename] = module; // Finally: the cache module caches according to the file name
    module.load(); // exports allows users to assign values to module.exports
    return module.exports; // The default is empty
}
let a = req('./a.js');
a = req('./a.js');
a = req('./a.js');
console.log(a)
Copy the code