The front end of the relevant knowledge -NodeJS] NodeJS comprehensive summary of the basics

NodeJS Basics

1. Node global objects and global variables

1.1 Global Object: All modules can be called

  1. global: The Node where the global environment, similar to the Object Browser window.

  2. process: The Node object represents the current process which allows developers to interact with the process.

  3. console: the built-in pointing Node console module, provides a command line environment standard input and standard output.

1.2 Global Functions

  1. Timer Functions: There are four, namely setTimeout (), clearTimeout (), setInterval (), clearInterval ();
  2. require: a loading module;
  3. Buffer (): operation for binary data.

1.3 Global Variables

  1. __filename: pointing to a script file name currently running.
  2. __dirname: point to the directory where the script is currently running.

2. Node three characteristics

2.1 single-threaded

    Node.js is not connected to each client to create a new thread, but only one thread. When a user is connected, it triggers an internal event, through a non-blocking I / O, event-driven mechanism, so that the macro program Node.js also parallel.
        

2.2 Non-blocking I / O  

  1. Since Node.js is adopted after the non-blocking I / O mechanism, so the code access to the database is performed, immediately behind the code is executed instead, the database returns the results of the processing code in the callback function, thereby improving the efficiency of the program.

  2. When an I / O is finished, the form will be event notification thread of execution I / O operations and thread callback function for this event. To handle asynchronous I / O, thread must have an event loop, constantly checking there are no pending events, to be addressed in turn.

  3. Blocking mode, a thread can only handle one task, in order to improve the throughput must be multi-threaded. The non-blocking mode, a thread always perform computing operations, the thread CPU core utilization is always 100%. So, this is a particularly philosophical solutions: its people, but a lot of people idle; your life is not like a person, Kill working children.

2.3 Event-driven event-driven

  1. In Node, in one moment, you can only execute a callback event, but an event callback function to perform in the middle, you can turn to other events (for example, another new user is connected), and then return to continue with the original event callback function, this processing mechanism, known as the "event loop" mechanism.

  2. Node.js is the underlying C ++ (V8 is written in C ++). Underlying code, the event queue for nearly half are constructed callback queue.

3. Node technical architecture

3.1 Node underlying architecture

nodejs part: v8 engine, libuv, builtin modules, native modules and other ancillary services.

Node Chart

  • v8 engine: There are two roles
    function 1. virtual machine execution js code (your own code, third-party code and native modules of code).
    2. Provide C ++ function interfaces for nodejs provide v8 initialization, create context, scope and so on.

  • libuv: It is based on event-driven asynchronous IO model library, our js code request, finalized by the libuv, and we set the callback function is triggered libuv.

  • builtin modules: It is written in C ++ code module consists of various types, including crypto, zlib, file stream etc basic functions. (V8 provides a function interface, libuv provide asynchronous IO model libraries, as well as some nodejs functions, serving builtin modules).

  • native modules: It is written in js, we provide a library application calls, but the modules are also dependent builtin modules to get the appropriate support services

[! NOTE]
Summary: If nodejs seen as a black box, since exposure to the interface developers are native modules, when we initiated the request, the request from top to bottom, through the native modules, through builtin modules will request to v8 , libuv and other ancillary services, end of the request, from the lower back first, we end up calling the callback function.

3.2 Node function call mechanism

Function call mechanism

  1. When v8 js code execution server.listen (), will pass some basic services to TCPWrap :: listen (), TCPWrap is nodejs built module by libuv of api uv_listen () way by libuv to complete the asynchronous call.

  2. Figure 1,2,3,4,5 step marked the call and return to the path, which is a few steps end soon, leaving a callback TCPWrap :: OnConnection () waiting for data needed to be ready after the call.

  3. libuv After obtaining the desired request, calls the callback TCPWrap :: OnConnection (), and finally call the JavaScript callback V8 engine in by tcp_wrap-> MakeCallback (env-> onconnection_string (), ARRAY_SIZE (argv), argv) in the function .

  4. Http Node.js built-in modules actually built on top of the net module. If you do net.js code found that the () Returns the class object by new TCP completing the subsequent TCP connect, bind, open socket like operation.

  5. We can see Node.js do the job like a bridge. Left V8, right libuv, 2 are connected together by the organic. For example HandleWrap :: HandleWrap () recorded in JavaScript objects and TCPWrap V8 instance of an object. So () in TCPWrap :: OnConnection can get these two objects, perform subsequent callback calls.


Reference article

Guess you like

Origin www.cnblogs.com/fecommunity/p/11922208.html