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, which is used to easily build fast and easy-to-scaling network applications. Node.js is made lightweight and efficient with event-driven, and is ideal 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. Many I/O operations must be carried out during the execution of the program, such as reading and writing files, input and output, request response, etc. Generally speaking, I/O operations are time-consuming. For example, in traditional programming mode, you need to read a file of several G, the entire thread will be paused and wait for the file to be read and continue to execute. In other words, I/O operations block the execution of code, greatly reducing the efficiency of the program.
Regarding asynchronous I/O, it is actually not unfamiliar to front-end engineers, because initiating an Ajax request 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 the way of initiating an Ajax request:
The code copy is as follows:
var fs = require('fs');
fs.readFile('/path', function(err, file) {
console.log('Read file completed');
});
console.log('Start reading file');
After the above code is called fs.readFile, the subsequent code is executed immediately, and the time when "read 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 simply send the I/O request to the operating system and continue to execute subsequent statements. When the operating system completes an I/O operation, the thread that performs the I/O operation is notified 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 a thread is constantly looping to detect the event queue. The event loop checks for unhandled events in the event queue until the end of the program. The programming method of events has the advantages of lightweight, loose coupling, and focusing only on transaction points. However, in the scenario of multiple asynchronous tasks, events are independent of each other, and how to cooperate is a problem. In Javascript, callback functions are everywhere, and callback functions are the best way to accept asynchronous calls to return data.
1.3 Single threaded
Node maintains the characteristics of JS as a single thread in the browser. The biggest advantage of single thread is that it does not require a state-conscious synchronization problem like multi-threaded programming, there is no deadlock, nor the overhead of thread context switching. Single thread also has its weaknesses, mainly manifested in three aspects: the inability to utilize multi-core CPU; errors will cause the entire application to exit, and the robustness of the application is worthy of the postgraduate entrance examination; a large amount of computing will occupy the CPU and make it impossible to continue calling asynchronous I/O.
To solve the above problem, Node adopts the same idea as HTML5 Web Workers and uses child_process to solve the problem of large amount of computation in a single thread. By distributing the calculations to each child process, a large number of calculations can be broken down and the results can be passed 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 the fact that Node has built a platform-level architecture libuv between the operating system and the Node upper-level module system.
2. Application scenarios
1) Real-time applications: such as online chat, real-time notification push, etc. (such as socket.io)
2) Distributed application: Use existing data through efficient parallel I/O
3) Tool application: massive tools, from front-end compression deployment (such as grunt) to 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 interface to improve web rendering capabilities
6) Unified front-end programming language environment: Front-end developers can quickly enter server-side development (such as the famous pure Javascript full-stack MEAN architecture)