My First Week of Reacquainting with Node.js – Node.js Style Features Read something about the underlying implementation this time:
  1. Chapter 3 Asynchronous I/ O-node. js How to Implement asynchronous I/O
  2. Udemy 《Learn and Understand NodeJS Learn and Understand NodeJS》 Section 2&3


V8 engine

First, the V8 engine is a must to learn Node.js, which compiles JS directly into machine code (which the processor can recognize).

To be more specific, V8 is a
  1. Open source
  2. Written in c + +
  3. Implement JavaScript according to the ECMA standard
  4. JavaScript can be compiled into machine code that the processor can recognize
  5. It can run independently
  6. Other C++ applications can also be embedded
JavaScript engine.



Node.js with V8 engine

Ordinary young people use V8: Run V8, JavaScript -> V8 – compile -> Machine Code
The literary youth had this bold idea:
Those ordinary JS methods are too boring, ability first. If I could write some C++ code and Add it to V8 as an add-on, V8 would be able to recognize more Javascript commands and be more powerful. When the user writes file.open(XXX) in js, you can use c++ to perform the file open function, so that your js (b b) is capable of handling files.


The artist’s idea is exactly what node.js is: a C++ application that builds the V8 engine into it, and that C++ application implements customized new features that make node.js perfect for server development.


What are the new features required for server development === node.js
  1. Manage reusable code
  2. Handle file
  3. Working with databases
  4. Internet communication
  5. Respond to request
  6. Tackle tasks that will take time to complete


Node. Js framework

  1. The first layer is C++ core, which is the customized features that are added (see event loop, libuv, and more in other explanations).
  2. The second layer is JS core, this layer is implemented with JS, based on/call C++ core, so that users can better use those C++ functions, but also to achieve many commonly used functions.
In the node.js source code, C++ core is in the SRC folder. JS Core is in the lib folder. This shows the hierarchical relationship between the two.




Asynchronous I/O Node. Js

How node.js implements asynchronous I/O, and what steps take place from JS to OS (this is a summary of udemy+ Depth)



  1. The js code you write calls the js core of Node.js (e.g. Fs: github.com/nodejs/node…). And you set up a callback function
  2. The JS core part calls C++ core (one of the fs.js calls.cc: github.com/nodejs/node…).
  3. C++ core calls libuv
  4. C++ core calls libuv’s methods to encapsulate the request object (the important intermediate in asynchronous I/O, from js to OS), which contains one of the most important things in asynchronous I/O: the callback function.
  5. Libuv sends the wrapped request object to the OS
  6. To the thread pool in the OS to be executed
  7. A thread in the thread pool executes the I/O operation contained in the request object that is sent, and the result is stored in the req->result attribute of the request object. Then submit the completed status, which is a notification that says, “I’m done!” , and then returns the thread to the thread pool
  8. The request object in the completed state is observed (two little people in the figure, different observers for different types of events) in the Event Loop. Large while loops, each called a tick, are presented (checked for Completed requests by methods in libuv) and placed on the Completed Events Queue
  9. The event loop retrieves the completed request object from the observer’s queue
  10. Fetch the result of the I/O operation and the callback function contained therein and send it to V8 for execution. This completes the purpose of calling the callback set up in #1.
note:

A. V8 engine execution of JavaScript is synchronous (sync) (stackoverflow.com/questions/2…). And a single thread
B. #1-#9 and #10 are working at the same time, I/O events are executed one by one, and then finally sent to the V8 engine one by one (since JS is synchronized) to execute the callback function
C. Thanks to B, the whole Node.js has the ability of asynchronous I/O
D. The event driven, non-blocking I/O characteristics can be explained. Event-driven is defined as #7-#10, where the event is executed through the following steps until the callback function is executed. Non-blocking I/O is the whole #1-#10 process. The JavaScript code we execute in V8 does not stop with the execution of the event. #1-#9 and #10 work simultaneously, one executing the I/O event and one being notified to execute the callback function.
E. Node.js is single-threaded/multi-threaded.


Next time I’ll write something about events and Event Emitters. Welcome to exchange, correct ~