Node. js features and application scenarios _ node. js-js tutorial

Source: Internet
Author: User
This article mainly introduces Node. this article describes the features and application scenarios of js. js's asynchronous IO, event loop and callback functions, single-thread, cross-platform features, and then summarizes its use scenarios. For more information, see Node. js should be one of the hottest technologies today. This article describes the features and application scenarios of Node. js.

Node. js is a platform based on Chrome JavaScript runtime. It is used to conveniently build fast and scalable network applications. With the help of event-driven Node. js, the non-blocking I/O model becomes lightweight and efficient, making it 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 are required during program execution, such as reading and writing files, input and output, and request responses. Generally, I/O operations are very time-consuming. For example, in the traditional programming mode, when you want to read a file of several GB, the entire thread is suspended and the execution continues after the file is read. In other words, I/O operations block code execution, greatly reducing program efficiency.

Asynchronous I/O is actually no stranger 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 similar to the method used to initiate an Ajax request:

The Code is as follows:


Var fs = require ('fs ');
Fs. readFile ('/path', function (err, file ){
Console. log ('file read complete ');
});
Console. log ('start reading file ');

After the above Code calls fs. readFile, the subsequent code is executed immediately, and the time when the "read file is complete" is unpredictable. When a thread encounters an I/O operation, it does not wait for the end of the I/O operation in blocking mode. Instead, it only sends the I/O Request to the operating system and continues to execute subsequent statements. When the operating system completes the I/O operation, the thread that executes the I/O operation is notified in the form of an event. The thread will process the event at a specific time.

1.2 event loops and callback Functions

The so-called event loop means that Node will solve all asynchronous operations using the event mechanism, and a thread is continuously detecting the event queue cyclically. The event loop checks whether there are any unprocessed events in the event queue until the program ends. The event programming method has the advantages of lightweight, loose coupling, and focus only on transaction points. However, in scenarios with multiple asynchronous tasks, events and events are independent. How to collaborate is a problem. In Javascript, callback functions are everywhere. Callback functions are the best way to receive data returned by asynchronous calls.

1.3 single thread

Node maintains the single thread feature of JS in the browser. The biggest benefit of a single thread is that you don't have to worry about State synchronization issues like multi-thread programming. There is no deadlock or thread context switching overhead. A single thread also has its weakness, mainly manifested in three aspects: failure to use multi-core CPU; error may cause the entire application to exit, and the robustness of the application is worthy of further study; A large amount of computing will occupy the CPU and thus cannot continue to call 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 computing workload in a single thread. By distributing computing to various sub-processes, a large number of computing tasks can be decomposed and then the results can be transmitted 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 because Node has built a platform-layer architecture libuv between the operating system and the Node upper-layer module system.

2. Application scenarios

1) Real-time applications: such as online chat and real-time notification push (such as socket. io)
2) distributed applications: use existing data through efficient parallel I/O
3) tool applications: massive tools, from front-end compression deployment (such as grunt) to desktop graphic interface applications
4) Game applications: The Gaming field has high requirements on 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 environment: front-end developers can quickly access server-side development (such as the famous pure Javascript full-stack MEAN Architecture)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.