Research and Application of Node.js

First of all, we need to know what is node.js?

Node.js uses Google's V8 engine, which is a server-side, non-blocking I/O, event-driven

JavaScript runtime environment.

1. What exactly does the Node.js architecture include? Let's see it more clearly from a picture.

natives modules

1. The content of the current layer is implemented by js.

2. Provide applications that can directly call libraries, such as fs, path, http, etc.

3. The js language cannot directly operate the underlying hardware settings.

builtin modules - glue layer :

The role of calling specific c++ functions

Bottom layer:

1. v8: Execute js code and provide bridge interface

Function: (self-written js code, built-in js code, third-party code)

Provide interface, [[ conversion function between js and c or c++

  1. libuv: event loop, event queue, asynchronous IO .
  2. Third-party modules: zlib , http , c-ares , etc.

2. The biggest feature of node.js is non-blocking IO, that is, asynchronous IO, so what is asynchronous IO?

From the figure below, let's take a look at what is asynchronous and synchronous.

What is the judgment condition for blocking IO: the operating system repeatedly calls the IO operation to determine whether the IO is over (polling). In layman's terms, it is a question of executing another process after one process is completed, or executing two processes at the same time. Common polling techniques include read, select, poll, kqueue, and event ports. Of course what we expect is non-blocking IO.

Libuv is a cross-platform event-driven asynchronous io library. But the functions he provides are not just io, including processes, threads, signals, timers, inter-process communication, etc. The following is an introduction to the Libuv architecture from the official website.

The current characteristics of asynchronous IO are:

1. IO is the bottleneck of the application

2. Asynchronous IO improves performance without waiting for the result to return

3. IO operations belong to the operating system system level, and the platforms have corresponding implementations

4. Node.js single thread cooperates with event-driven architecture and libuv to realize asynchronous IO

3. Event loop

NodeJs works in an event-driven model, involving an event demultiplexer (Event Demultiplexer)  and an event queue (Event Queue) . All I/O requests will eventually generate a success or failure event or other trigger, called an event (Event) . These events are processed according to the following algorithms.

1. The event demultiplexer accepts  I/O requests and delegates these requests to the appropriate hardware.

2. Once the I/O request is processed (for example: data in a file can be read, data from a socket can be read, etc.), the event demultiplexer is a specific behavior in a queue that needs to be processed Add registered callback handlers. These callbacks are called events, and the queue to which events are added is called the event queue .

3. When there are events that can be processed in an event queue, they will be executed in the order in which they are accepted until the queue is empty.

4. If there are no events in the event queue or no pending requests from the event demultiplexer, the program will complete. Otherwise, the process continues from step one.

The program that coordinates the entire mechanism is called the event loop (Event Loop ) .

4. Event-driven architecture

Event-driven architecture is a common pattern in software development. It is similar to publish-subscribe and observer. They have a common feature: the main body publishes messages, and other instances receive messages .

Nodejs provides a dedicated module for events: lib/events.js. There are two commonly used methods for events, namely on and emit, on is used to listen to events, and emit is used to trigger events.

5. Node.js Application Scenarios

1. IO-intensive high concurrent requests

2. Operate the database to provide API services

3. Live Chat Apps

6. What are the steps for Node.js to implement API services?

1. npm init -y to initialize the project.

2. npm install -g typescript to install typescript globally.

3. tsc --init generates the tsconfig.json configuration file.

4. npm install -g ts-node runs the ts file during development.

5. Write the requirements code.

6. Import express npm i express type npm i @types/express -D.

Guess you like

Origin blog.csdn.net/qq_43185384/article/details/129140182