Home > Web Front-end > JS Tutorial > body text

Introduction to the characteristics and application scenarios of Node.js

高洛峰
Release: 2017-02-04 10:16:12
Original
1327 people have browsed it

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:

var fs = require('fs');
fs.readFile('/path', function(err, file) {
    console.log('读取文件完成');
});
console.log('开始读取文件');
Copy after login

The above code is after calling 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 is constantly looping to detect 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 amount of calculation in a 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.

Introduction to the characteristics and application scenarios of Node.js

2. Application scenarios

1), real-time applications: such as online chat, real-time notification push, etc. (such as socket.io)
2 ), distributed applications: using 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 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), using stable interfaces to improve Web rendering capabilities
6), front-end and back-end programming languages Unified environment: Front-end developers can quickly switch to server-side development (such as the famous pure Javascript full-stack MEAN architecture)

For more information on the characteristics and application scenarios of Node.js, please pay attention to PHP Chinese. net!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template