1. What is node
Nodejs is a JavaScript runtime environment based on Chrome V8. It enables JS to run on the server and across platforms (Windows, MAC OS, and Linux). In addition, unlike other back-end languages such as PHP, Node has a built-in HTTP server and does not require additional HTTP server deployments such as Apache
2. Node features
- Non-blocking I/O
In Node, when a thread encounters an I/O operation, it does not wait for the I/O operation to complete in blocking mode (traditional synchronous I/O mode, where only one thing can be done at a time). Instead, it forwards the I/O request to the operating system and continues to execute the next command. When the IO operation is complete, the event notification thread comes back to handle the event. This allows you to continuously “take orders” and process many I/O requests
- event-driven
The event-driven core is a combination of asynchronous callback functions (the core is the non-blocking IO above) and event loops. Node generates an event loop to listen for events, constantly checking for unprocessed events (called event polling). The callback function is called when an event is triggered
- Single thread
In order to achieve high concurrency, the traditional synchronous IO requires multiple threads. In the case of multi-core CPU, multi-core resources can be effectively utilized. However, due to the time-consuming IO operation, the performance of multi-thread scheme is often low. Node, on the other hand, is based on a single-threaded mode of non-blocking I/O, with a CPU core of 100% at all times and asynchronous processing of I/O manipulation. Avoids the overhead and complexity of thread switching, creation, and destruction of multiple threads. High concurrency can be handled with high performance through asynchronous callbacks and event-driven models.
At the same time, the weakness of single-threading is that it cannot take advantage of multi-core cpus because there is only one event loop. In the case of CPU-intensive tasks, multi-core CPU resources are wasted
3. What is Node good at
(1) Front-end engineering application
Build tools, such as WebPack, focus on improving efficiency and quality for developers. These applications are services running in a local environment that can quickly locate, analyze, and resolve problems
(2) Back-end service applications
Such as Express, KOA, Egg and other Node server-side frameworks, their audiences are real users, providing users with data processing in terms of interactive experience. Such applications run on remote services and require tools to monitor health. We focus on providing stable, secure and high performance services. This kind of application really gives full play to the asynchronous driving characteristics of Node. It mainly shows that when the main thread is not affected by CPU intensive, it gives full play to its characteristics of handling high concurrency and becomes the back-end service with high I/ O in most networks
(3) Quasi-real-time system
Such as chat system, blog social system and a class of lightweight, high traffic, no complex computing system.
(4) Game server
3. What is Node not good at
Node.js is not good at processing CPU-intensive services (such as data encryption, compression, etc.). If you want to realize a CPU consuming computing logic, you can use the network asynchronous I/O to other processes (specifically, several Node services are respectively responsible for computing). The master Node server process then gets the final calculation through an asynchronous request.
Asynchronous network I/O makes full use of the asynchronous event-driven capability of Node.js to transfer the time-consuming CPU calculation logic to other processes without waiting for time-consuming CPU calculation, and can directly process other requests or other logic.