1, what is nodejs
1) Node.js is a JavaScript operating environment of the basic Chrome V8 engine
2) Node.js uses a model of event driving and non -blocking I/O to make it lightweight and efficient
3) Node.js’s package manager NPM (Node Package Mange) is the world’s largest open source library ecosystem
2, global variable
__diRName: The path of the current file
__filename: The absolute path of the current file
Console: The console object, you can output information
PROCESS: Objects of the current execution process, you can obtain the relevant information of the process, environment variables, etc.
settimeOut/Cleartimeout: delay execution, time is executed, only once executed
Setinterval/Clearinterval: timer, after a period of time, execute once every time
3, FS module
fs.stat/fs.statsync: Visit the metadata of the file, such as the size of the file, the modification time of the file
fs.readfile/fs.readfilesync: asynchronous/synchronous reading file
FS.Writefile/FS.WriteFilesync: asynchronous/synchronous writing file
fs.reamdir/fs.Readdirsync: Read the contents of the folder
fs.unlink/fs.unlinksync: Delete file
fs.rmdir/fs.rmdirsync: can only delete the empty folder
4. Talk about the incident cycle of EventLoop
1) All synchronous tasks are performed on the main thread, forming an execution stack
2) When the execution stack in the main thread is empty, check whether the event queue is empty, if it is empty, continue to check; if it is not empty, execute 3
3) Take out the first part of the task queue and join the execution stack
4) Execution task
5) Check the execution stack. If the execution stack is empty, jump back to Step 2; if it is not empty, continue to check
5. Briefly describe the difference between synchronization and asynchronous, how to avoid callback hell
Synchronization method Calling Once the caller must wait until the method is called and returns before the follow -up behavior can be continued.
Once the
asynchronous method calls start, the method call will return immediately, and the caller can continue the subsequent operation. The asynchronous method is usually in another thread. The entire process will not hinder the work of the caller.
Avoid callback hell:
1)Promise
2)async/await
3)generator
4) Event release/listening mode