Node.js should be one of the hottest technologies today. This article mainly introduces the characteristics and application scenarios of Node.js.
Node.js is a platform built on the Chrome JavaScript runtime to easily build fast and easily scalable web applications. Node.js uses event-driven, non-blocking I/O models to become lightweight and efficient, which is very suitable for data-intensive real-time applications running on distributed devices.
1. Features
1.1 Asynchronous I/O
The so-called asynchronous I/O is relative to synchronous I/O. During program execution, many I/O operations must be performed, such as reading and writing files, input and output, request responses, etc. Generally speaking, I/O operations are very time-consuming. For example, in the traditional programming model, if you want to read a file of several gigabytes, the entire thread will pause and wait for the file to be read before continuing. In other words, I/O operations block the execution of code and greatly reduce the efficiency of the program.
Regarding asynchronous I/O, it is actually not unfamiliar to front-end engineers, because initiating Ajax requests is the most common "asynchronous" call. In Node, taking reading a file (reading a file is a time-consuming I/O operation) as an example, it is very similar to how to initiate an Ajax request:
After the above code calls fs.readFile, the subsequent code is executed immediately, and the moment when "reading the file is completed" is unpredictable. When a thread encounters an I/O operation, it will not wait for the I/O operation to end in a blocking manner, but will just send the I/O request to the operating system and continue to execute subsequent statements. When the operating system completes the I/O operation, it notifies the thread performing the I/O operation in the form of an event, and the thread will process the event at a specific time.
1.2 Event loop and callback function
The so-called event loop means that Node will use the event mechanism to solve all asynchronous operations, and there is a thread that continuously checks the event queue. The event loop checks the event queue for unhandled events until the program ends. The event programming method has the advantages of being lightweight, loosely coupled, and focusing only on transaction points. However, in the scenario of multiple asynchronous tasks, events are independent of each other, and how to collaborate is a problem. In Javascript, callback functions are everywhere, and callback functions are the best way to receive data returned from asynchronous calls.
1.3 Single Thread
Node maintains the single-threaded characteristics of JS in the browser. The biggest advantage of single-threading is that you don't have to worry about state synchronization issues like multi-threaded programming. There is no deadlock, and there is no overhead of thread context switching. Single-threading also has its weaknesses, mainly in three aspects: it cannot utilize multi-core CPUs; errors will cause the entire application to exit, and the robustness of the application is worthy of examination; a large amount of calculations will occupy the CPU and make it impossible to continue calling asynchronous I/O.
In order to solve the above problems, Node adopts the same idea as HTML5 Web Workers, using child_process to solve the problem of large calculation amount in single thread. A large number of calculations can be broken up by distributing calculations to various sub-processes, and then communicating the results through event messages between processes.
1.4 Cross-platform
Node is cross-platform, that is, the same set of JS code can be deployed and run on Windows, Linux, OSX and other platforms. This is mainly due to Node building a platform layer architecture libuv between the operating system and Node's upper module system.
2. Application scenarios
1), real-time applications: such as online chat, real-time notification push, etc. (such as socket.io)
2) Distributed applications: use existing data through efficient parallel I/O
3) Tool applications: A large number of tools, ranging from front-end compression deployment (such as grunt) to large desktop graphical interface applications
4) Game applications: The game field has high requirements for real-time and concurrency (such as NetEase’s pomelo framework)
5). Use stable interfaces to improve web rendering capabilities
6) Unified front-end and back-end programming language environments: Front-end developers can quickly switch to server-side development (such as the famous pure Javascript full-stack MEAN architecture)