Introduction of the Node

1. The birth history of Node

Node was born in March 2009.

2.Node naming and origin

Aliases of Node: Nodejs, Nodejs, node.js.

2.1 Why JavaScript

Key points of designing high-performance Web servers: event-driven, non-blocking I/O.

JavaScript is the implementation language for Node for three main reasons: high performance, event-driven, and no historical baggage.

2.2 Why Node

Node can build servers, clients, and command-line tools. Node is developed as a single-threaded, single-process system that forces no sharing of any resources. Its purpose is to become a fast and scalable platform for building web applications.

Node itself is very simple, organizing many nodes through communication protocols, making it easy to scale to build large network applications.

Each Node process is built as a Node of the network application, which is the true meaning of its name.

3. What Node brings to JavaScript

Node breaks the tradition of running JavaScript only in the browser and provides a unified programming environment for both the front and back ends, which greatly reduces the cost of context exchange for the front and back ends.

4. The characteristics of the Node

As a platform for running back-end JavaScript, Node retains the familiar interfaces of the front-end browser JavaScript, without changing any of the language’s features, and still relies on scopes and prototype chains. The difference is that it migrates the ideas widely used in the front-end to the server side.

4.1 the asynchronous I/O

The capture of the result value in an asynchronous call conforms to the “Don’t call me, I will call you” principle.

In Node, most operations are invoked asynchronously.

4.2 Events and callback functions

The event programming approach has the advantages of being lightweight, loosely coupled, and focused only on transaction points.

Callback functions are everywhere, and in JavaScript, functions are first-class citizens that can be passed as objects to methods to call as arguments.

4.3 single thread

In Node, JavaScript cannot share any state with other threads.

Advantages of single threading:

  1. You don’t have to worry about state synchronization like multiprocess programming does;
  2. No deadlocks;
  3. There is no performance overhead of thread context exchange;

Disadvantages of single threading:

  1. Unable to utilize multi-core CPUS
  2. Errors can cause the entire application to quit, and the robustness of the application is worth testing;
  3. A large number of computations occupy the CPU, causing asynchronous I/O calls to fail.

Web Works has the ability to create worker threads to perform computations to solve the problem of large JavaScript computations blocking UI rendering.

In order not to block the main thread, the worker thread passes the results of the run through message passing, which also prevents the worker thread from accessing the UI in the main thread.

Node handles heavy computation in a single thread: child_process.

Child processes: By distributing computations to child processes, you can break down a large number of computations and then pass the results through event messages between processes, which is a good way to keep the application model simple and low dependency.

4.4 cross-platform

Node runs on Windows and * Nix platforms thanks to a platform-layer architecture, called Libux, between the operating system and Node’s upper module system.

5. Application scenarios of Node

Node applies to I/O – intensive and CPU – intensive scenarios.

5.1 I/O intensive

Node is network-oriented and good at parallel I/O, which can effectively organize more hardware resources to provide more and better services.

I/O intensive advantages:

Taking advantage of the ability to cycle events, rather than starting every thread to service every request, the resource footprint is minimal.

5.2 Are you not good at CPU intensive services

Node’s superior computing power comes from V8’s deep performance optimizations.

CPU intensive applications present Node challenges:

Due to single-threaded JavaScript, long running computations (such as large loops) will result in CPU time slices that cannot be freed and subsequent I/ OS cannot be initiated. However, the large operation task can be properly adjusted and decomposed into several small tasks, so that the operation can be timely released without blocking the initiation of I/O calls. In this way, the benefits of parallel asynchronous I/O can be simultaneously benefited, and the CPU can be fully utilized.

Node makes full use of the CPU in the following ways:

  1. By writing C/C++ extensions to efficiently use the CPU, some V8 can not achieve the ultimate performance through C/C++ to achieve;
  2. Separate computation and I/O by means of child processes, using inter-process messaging results;

CPU density is not terrible, how to properly schedule is the trick.

5.3 Coexist peacefully with legacy systems

The old system has very stable data output and continues to serve traditional websites while providing data sources for mobile versions. Node uses this data source as a data interface to take advantage of asynchronous parallelism.

5.4 Distributed Application

Alibaba’s data platform.

5.5Node users

Users of Node:

  1. Unified front-end and back-end programming environment; (yahoo!)
  2. Node provides high performance I/O for real-time applications. (Tencent, Petal.com, Mogujie)
  3. Parallel I/O allows consumers to leverage distributed environments more efficiently; (Alibaba, eBay)
  4. Parallel I/O, effective use of stable interface to improve Web rendering ability; (Snowball Finance, LinkedIn)
  5. Cloud computing platform provides Node support; (Microsoft, Aliyun, Baidu Cloud)
  6. Game development; (netease)
  7. Tool applications;