Home  >  Article  >  Web Front-end  >  What is Node.js? What are the advantages?

What is Node.js? What are the advantages?

青灯夜游
青灯夜游forward
2021-01-06 17:51:062583browse

What is Node.js? What are the advantages?

Related recommendations: "nodejs Tutorial"

As a loyal fan of JS, although JS does not have as big an ecosystem as JAVA , but JavaScript has moved from client-side scripts to server-side, which makes us pay attention to it. JavaScript is slowly rising, and we need to learn it from a different perspective.

What is Node.js?

Nodejs is essentially a JavaScript running environment.

The V8 engine based on Google is actually an execution environment that is cut out of the V8 engine from Chrome and encapsulated.

Let’s go to the official website to download the package and install it in a fool-proof way.

Node download official website: https://nodejs.org/en/download/

Enter node on the command line to run the javaScript script.

Advantages of Node.js? Why can the scripting language JavaScript be used as a server language?

Advantage 1 Node.js has super high concurrency capabilities

The first goal of Node.js is Provides a simple development tool for creating high-performance servers and running various applications on the servers.

Compared with classic server-side languages ​​​​such as Java, PHP or .net, Node.js is like a young and energetic boy. The Java language will create a new thread for each client, and each A client connection creates a thread, which consumes 2MB of memory. That is to say. Theoretically, an 8GB server can have about 4,000 concurrently connected users. To support more users with high concurrency, additional servers must be added.

          Node.js does not create a new thread for each client connection, but only uses one thread.

This is because Node is based on a single thread (there is only one main thread to receive requests and respond)

Isn’t this slower? In fact, this is not the case.

When Node.js receives a user connection, it will trigger an internal event. Respond to user behavior through pre-defined functions. The main thread of Node.js does not care about the process of the program. In fact, there is another working thread to help the main thread of Node to access files and read the database. When the working thread reads the file data or the data in the database, The callback function will be returned to the Node main thread for execution, such as transmitting the found data back to the client and closing the connection for some operations. (This is Node non-blocking I/O, event-driven).

        Attached below is a picture I drew (please forgive me if there is any inappropriateness)

                       There should be a prototype in your head, that is - the main thread of Node.js has been busy receiving requests and responding to requests, so that it can continuously receive requests from multiple clients, and it does not have to wait stupidly. In IO operations, when the IO worker thread finds the data, it will trigger the event callback function to tell the main thread that the data has been obtained. At this time, the main thread will execute the callback function and return the data to the client.

Theoretically, a server with 8G memory can accommodate the connections of 30,000 to 40,000 users at the same time.

This is where Node shines (single-threaded, non-blocking IO, event-driven)

Advantages 2 Node uses JavaScript syntax

Node.JS The V8 engine is based on javaScript, which means that as long as you know the syntax of JS, you can use it for back-end development, but Node officially recommends ECMA Script6 (ES6) syntax .

Node breaks the past situation where JavaScript can only run in the browser, unifying the front-end and back-end programming environments, which greatly reduces development costs. (This is very friendly to front-end developers. The more things JS can do, the faster the front-end development will be)

Advantages 3 The emergence of Node.JS promotes front-end engineering Thoughts

The front-end has developed rapidly in recent years, and Node.js plays an irreplaceable role.

Node.js is not only a running environment, but also a completely new JavaScript language. It accommodates the basics of JS syntax and also adds some internal modules

Such as:

http is used Module for processing request response

                                                                                                use       using           using         use ’ s ’ s ’ through using ’ s through through ’s ’ through through through ‐ ‐ to ‐ ‐ ‐ n , ‐ and ‐ ‐ to 1 to 1 , Let us be convenient to download the bags dependent on our project through the command line

::

## NPM Install jQuery

## NPM Install -G Webpack

# NPM Install-G Create-React-APP, etc. Define module syntax ↓↓

const http = require('http')
 const  Jquery = require('jquery');

                                                                                                        . Make front-end engineering modular, develop quickly, reduce code coupling, and greatly enhance maintainability.

What is Node.js suitable for? This is listed below a few node.js application scenarios

## Although there are so many node.js listed above advantages, but no language is without disadvantages, and Node is no exception. It can only be a matter of what scenarios and fields a language is suitable for.

Let’s talk about the scenarios where Node.js is not suitable (add a big title ↓↓↓)

Node is not suitable for Scenario

①CPU calculation-intensive program

Before Node.js version 0.8, Node.js did not support multi-threading. Of course, this is a matter of design philosophy, as Node.js developers and supporters firmly believe that single-threaded and event-driven asynchronous programming runs more efficiently than traditional multi-threaded programming. But in fact, multi-threading can achieve the same throughput, although it may be expensive, but there is no need to make special configurations for multi-core environments. In contrast, due to its single-threaded nature, Node.js must use a multi-process approach to fully utilize multi-core resources.

Ideally, Node.js single-thread will completely occupy a CPU core during execution, and all requests must wait for the current request to be processed and enter the event loop before responding. If an application is compute-intensive, then unless you manually break it up, the request response latency will be considerable. For example, if a complex calculation needs to be performed in the callback function of an event and takes up 200 milliseconds of CPU, then all requests in the event loop will have to wait for 200 milliseconds. In order to improve response speed, your only way is to split this computationally intensive part into several logics, which brings additional complexity to programming. Even so, the total throughput and total response latency of the system will not be reduced, but the scheduling will be slightly fairer. Fortunately, in a real Web server, there is rarely a computationally intensive part, and if it is, it should not be implemented for immediate response. The correct way is to give the user a prompt that the server is processing, notify the user when it is completed, and then leave it to other processes on the server or even other dedicated servers to do the work.

②Single-user multi-tasking application

What we discussed earlier is usually server-side programming, and one of the assumptions is that there are a large number of users. But if you are facing a single user, such as a local command line tool or graphical interface, then the so-called large number of concurrent requests does not exist. So another terrible problem arises. Although it is a single user, it is not necessarily a single task. For example, while providing an interface to the user, a certain calculation is being performed in the background. In order to prevent the user interface from blocking, you have to enable multi-threading or multi-process. The communication between Node.js threads or processes is so far inconvenient because it has no locks at all, so it is said to not be deadlocked. The multiple processes of Node.js often perform the same task and utilize the resources of multiple processors through multiple processes. However, when multiple processes cooperate with each other, it becomes stretched.

③Transactions with very complex logic

The control flow of Node.js is not linear. It is broken up by events, but human thinking is linear. When you try to convert your thinking to cater to the language or compiler, sacrifices have to be made. For example, you have to implement a logic like this: withdraw money from the bank, use the money to purchase a certain virtual product, and add it to the inventory database after purchasing. Any step in between may involve dozens of I/O operations. , and a rollback operation must be performed after any operation fails. This process is linear and already very complex. If it were to be split into non-linear logic, the complexity would probably be unmaintainable. Node.js is better at handling tasks with simple logic but frequent access, but is not suitable for completing tasks with very complex logic.

“Okay, that’s my summary of Node.js. I won’t say anymore, I’ve been writing it for a long time, so I’m going to code it.”

For more programming-related knowledge, please Visit: programming teaching! !

The above is the detailed content of What is Node.js? What are the advantages?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete