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?

Dec 20, 2021 am 10:13 AM
node.js

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Let's talk about how to choose the best Node.js Docker image? Let's talk about how to choose the best Node.js Docker image? Dec 13, 2022 pm 08:00 PM

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Node.js 19 is officially released, let's talk about its 6 major features! Node.js 19 is officially released, let's talk about its 6 major features! Nov 16, 2022 pm 08:34 PM

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Let's talk about the GC (garbage collection) mechanism in Node.js Let's talk about the GC (garbage collection) mechanism in Node.js Nov 29, 2022 pm 08:44 PM

How does Node.js do GC (garbage collection)? The following article will take you through it.

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

Learn more about Buffers in Node Learn more about Buffers in Node Apr 25, 2023 pm 07:49 PM

At the beginning, JS only ran on the browser side. It was easy to process Unicode-encoded strings, but it was difficult to process binary and non-Unicode-encoded strings. And binary is the lowest level data format of the computer, video/audio/program/network package

See all articles