Home >Web Front-end >Front-end Q&A >What is node.js? What are the applicable scenarios?

What is node.js? What are the applicable scenarios?

藏色散人
藏色散人Original
2021-12-20 10:13:303960browse

node.js is a platform built based on Chrome JavaScript runtime. Its applicable scenarios include localized online music applications, localized online search applications, localized online APPs, etc.

What is node.js? What are the applicable scenarios?

The operating environment of this article: windows7 system, nodejs10.16.2 version, Dell G3 computer.

What is node.js?

Simply put, Node.js is JavaScript running on the server side.

Node.js is a platform built on the Chrome JavaScript runtime.

Node.js is an event-driven I/O server-side JavaScript environment based on Google's V8 engine. The V8 engine executes Javascript very quickly and has very good performance.

Understand the applicable scenarios of NodeJS in principle

NodeJS is a popular server-side JS platform in recent years. This is due to its high performance in back-end processing. The excellent performance of concurrency, on the other hand, the rise of powerful code and project management applications such as npm, grunt, and express on the nodeJS platform has almost redefined the working methods and processes of the front-end.

The success of NodeJS marks its power, but are not all situations suitable for using NodeJS as a server-side platform?

The answer is of course no, and there are different opinions on the Internet. Then let’s understand the application of NodeJS from the principle.

Before talking about NodeJS, let’s first take a look at the traditional (represented by Apache) The way the server-side processing platform handles concurrency .

1. Apache’s multi-threaded high concurrency mode

Apache is currently the world’s number one Web server software. Because it supports multi-thread concurrency, it is welcomed by the majority of server technology selectors. But later on, Apache gradually exposed its shortcomings in some large-scale WEB applications: blocking.

Some students may wonder, isn’t Apache multi-threaded for concurrent processing? Why is there still blocking?

To understand this we first need to understand the concept of thread

1.1 What is a thread?

We quote the official explanation: The smallest CPU unit that threads can run independently can run concurrently in the same process and share the memory address space under the process(note this feature).

We can see that threads under the same process will share the same files and memory (memory address space), so you can imagine that when different threads need to occupy the same variable, they will be determined based on the first arrival. Based on the first-come-first-serve principle, when the thread that arrives first is running, subsequent threads can only wait aside, that is, they join the blocking queue sequence. So this is what causes the thread to block.

Therefore, although a process can support multiple threads, they appear to execute simultaneously, but are not synchronized with each other. Multiple threads in a process share the same memory address space, which means they can access the same variables and objects, and they allocate objects from the same heap. Although this makes it easier to share information between threads, the programmer must be careful to ensure that they do not interfere with other threads in the same process.

After understanding the shortcomings of multi-threaded parallelism, we can better understand the power of NodeJS. Because NodeJS is asynchronous and single-threaded!

2. Asynchronous I/O principle of NodeJS

Let’s first look at a piece of code for Apache to request the database:

When the code is executed to the first line, the thread will block, wait for the query to return the result, and then continue processing. Due to reasons such as database query, disk reading and writing, network communication (so-called I/O), the blocking time will be very large (relative to the total CPU frequency). For high-concurrency access, on the one hand, threads are blocked and waited for a long time, on the other hand, new threads are constantly added to cope with new requests, which will waste a lot of system resources. At the same time, the increase of threads will also take up a lot of CPU time to process the memory context. switch. Let’s see how node.js handles it.

See, is just four words: asynchronous callback. The second parameter of query is a callback function. When the process executes db.query, it will not wait for the result to be returned, but will directly continue to execute the following statements until it enters the event loop. When the database execution result returns, the event will be sent to the event queue, and the previous callback function will be called only after the thread enters the event loop. A more technical term is asynchronous I/O. Just a single thread is fine.

So why can NodeJS be single-threaded but can achieve asynchronous implementation? Here we go to the previous picture and click on the Event queue

in the picture.

Did you see that the working principle of NodeJS is actually event loop. It can be said that every piece of NodeJS logic is written in the callback function, and the callback function is executed asynchronously after returning!

Seeing this, you can't help but wonder, if all processing of NodeJS is asynchronous, wouldn't it be a success? Wrong, wrong, wrong! Of course not, don't forget, the basis for NodeJS to implement these is single thread. That's right, single threaded! One thread handles all operations!

You can imagine that NodeJS is facing 100,000 concurrent troops in the cold wind. OK, no problem. One enemy comes up and is thrown into the city, and the other comes up and is thrown into the city. All the people in the city are soldiers and can digest these enemies very well. But if a character like Zhang Fei and Zhao Yun comes up, the old Node will feel miserable. He fights Zhang Fei for 300 rounds, cripples him, and then throws him into the city. Then the 100,000 troops behind will have to wait for these 300 rounds. . .

So what does this mean? It means that NodeJS does not block, but blocking does not occur in the subsequent callback process, but occurs in the calculation and processing of logic by NodeJS itself. We already know that NodeJS has extremely powerful distribution capabilities and can loop events for asynchronous callbacks. But if you encounter complex logical operations when looping events, how can a thin single thread support millions of logical concurrencies? All time-consuming operations of NodeJS such as I/O and network communication can be handed over to worker threads for execution and callback, so it is very fast. But for the normal operation of the CPU, it can only resist on its own.

Speaking of which, you probably have a rough idea of ​​the features of NodeJS. So the applicable scenarios are basically ready to come~!

3. Application scenarios of NodeJS

Since NodeJS has strong ability to handle concurrency, but weak ability to handle calculation and logic, therefore, if we put complex Logical operations are moved to the front end (client) to complete, and NodeJS only needs to provide asynchronous I/O, so that high-concurrency and high-performance processing can be achieved. There are many situations, such as: RESTFUL API, real-time chat, single-page APP with strong client logic. Specific examples include: localized online music application, localized online search application, localized online APP, etc.

By the way, Apache has been suppressed so much, so give me a sweet date. Apache has the characteristics of multi-threaded high concurrency shared memory address space, which means that if the server is powerful enough and the processor has high enough cores, Apache will operate very well, so it is suitable for (concurrent) asynchronous processing with relatively few backgrounds. Applications that require heavy calculations and complex backend business logic.

Recommended learning: "node.js Video Tutorial"

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn