Why Node.js?

Continue to create, accelerate growth! This is the second day of my participation in the "Nuggets Daily New Plan · June Update Challenge", click to view the details of the event

Why Node.js?

We all know that Node.js was born to implement high-performance web servers, and then Node.js slowly evolved into a server-side "language".

Why is Node.js the best of the many solutions?

Let's take a look at the whole process of the user from sending the request to getting the dataimage.png

  1. The client initiates a request according to the user's operation
  2. The server responds to the corresponding request according to the business logic

Here we put aside objective factors such as bandwidth, hardware, and performance. What really affects the speed at which users obtain data is: IO time consumption . And we all know that IO is the slowest link in the computer operation process (the consumption in disk and network is millisecond level).

Assumption

At present, it is a serial task, and one of the requests being processed has an IO behavior that needs to be processed for a long time, so the subsequent tasks cannot get a timely response.

At present, many advanced editing languages ​​adopt multi-process and multi-thread methods for processing, but there will be some problems.

Here we use restaurants as an example:

  • There are currently several guests ordering (several requests made)
  • There are several servers to serve (several threads to respond to the request)

This is a very good experience for guests, but if a lot of guests come in during a certain period of time, our threads cannot increase unlimitedly, so the process of no response will appear.

Reactor pattern

Based on this situation, there is the Reactor pattern, which is the Responder pattern.

His core idea is to keep only one waiter, then let the guests order by themselves, and call the waiter after the guests have ordered.

In this case, a single thread is used to complete multi-threaded things, and it is non-blocking. Using this operation can avoid problems such as state preservation and time consumption when multiple threads are switched.

Node.js is based on the Reactor mode and the characteristics of the js language itself, so that the single thread is far away from blocking and can use sql support through asynchronous non-blocking IO.

What can Node.js do?

  • Lightweight, high-performance web services
  • Front-end and back-end JavaScript isomorphic development
  • Convenient and efficient front-end engineering

Node.js Architecture

Here we divide the core of Node.js into three major parts

  • Natives modules: Expose the corresponding interface for developers to use
  • Builtin modules: Help node.js find the corresponding C++ call interface
  • Specific function layer: responsible for the final execution of js code, and deal with the details of code execution in the node environment

Natives modules

  • The content of the current layer is implemented by JS
  • Provide applications that can directly call libraries, such as fs, path, http, etc.
  • The JS language cannot directly operate the underlying hardware devices

Because the JS language cannot directly operate the underlying hardware devices , we need the corresponding middle layer to forward events for us Builtin modules came into being.

Builtin modules glue layer

Through this middle layer, we can let the core modules of Node.js to obtain lower-level operations, such as file read and write behavior.

specific functional layer

This layer is some specific functional modules:

  • v8: Execute js code, provide bridge interface
  • libuv: event loop, event queue, asynchronous IO
  • c-ares(DNS)
  • http parser
  • zlib (compression)
  • Wait...

image.png

Guess you like

Origin juejin.im/post/7102036641711128589