preface

If you have some front-end basics, such as HTML, CSS, JavaScript, jQuery; Node.js, then, allows you to quickly and cheaply transition to being a full stack engineer (what I call a pseudo-full stack, and what I consider a full stack is database savvy) to touch both back-end and mobile development. Of course, Node.js is not a panacea, nor does it mean that learning it can completely replace other backend development languages. It has its own mission and application areas of expertise.

In addition, many data layer dynamic interactions such as Vue. Js and React.js are preferred by Node.js, as are some popular packaging tools. All this adds up to one more reason why you should study it.

Node.js has its own strengths and weaknesses compared to traditional back-end languages (such as PHP and JAVA), with different areas of expertise and focus. Therefore, each has its own strengths and needs. Node.js gives us an easy way to do back-end development. So we should not always say which language is the best, each has his own mission, heh heh.

The characteristics of the Node. Js

Non-blocking asynchronous IO

For example, when accessing a database to retrieve data, it takes a while. In traditional single-threaded processing, after the database access code has been executed, the entire thread will pause and wait for the database to return the results before executing the subsequent code. In other words, I/O blocks the execution of code, greatly reducing the efficiency of program execution.

Because of the non-blocking I/O mechanism in Node.js, after executing the code that accesses the database, it immediately moves to execute the following code, putting the code that returns the result from the database into the callback function, thus improving the execution efficiency of the program.

When an I/O completes, the thread that performed the I/O operation is notified as an event, and the thread executes the callback function of the event. To handle asynchronous I/O, a thread must have an event loop that continuously checks for unhandled events and processes them in turn.

In blocking mode, a thread can only handle one task, and multithreading is required to improve throughput. In non-blocking mode, a thread is always performing computations, and the CPU core utilization of that thread is always 100%. So this is a particularly philosophical solution: instead of having too many people, but too many of them idle; It’s better to die alone, working to death.

Single thread

In server-side languages such as Java, PHP, or.NET, a new thread is created for each client connection. Each thread consumes about 2MB of memory. In other words, the maximum number of users that can be connected to a server with 8GB of memory is about 4000. To enable Web applications to support more users, the number of servers needs to increase, and the hardware cost of Web applications certainly goes up.

Instead of creating a new thread for each client connection, Node.js uses only one thread. When a user connects, an internal event is triggered, making Node.js programs macroscopically parallel through non-blocking I/O, event-driven mechanisms. With Node.js, a server with 8GB of ram, it can handle connections from over 40,000 users at the same time.

In addition, the benefit of single threading is that the operating system completely eliminates the time overhead of thread creation and destruction. However, single threading also has many disadvantages, which will be explained in more detail in Node.js.

event-driven

In Node.js, events are triggered when a client requests to establish a connection or submit data. In the Node. Js, in a moment, can only perform one event callback function, but in the middle of the execute an event callback function, and other events, can turn to handle other events (for example, have a new user connection), and then returned to continue to perform the original event callback function, the processing mechanism, called the “ring” mechanism.

Node.js is C++ (V8 is also written in C++). Nearly half of the underlying code is devoted to building event queues and callback function queues. Using event-driven to complete the task scheduling of the server, this is a genius. The dance on the head of a pin, with one thread, takes on the task of handling a very large number of tasks.

Note the event loop, which is the essence of Node.js. Here’s a quote from the node.js website

┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ > │ timers │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ pending Callbacks │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ idle, Prepare │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ incoming: │ │ │ poll │ < ─ ─ ─ ─ ─ ┤ connections, │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ data, Etc. │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ │ check │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ └ ─ ─ ┤ close callbacks │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘Copy the code

Here’s a quote from the Node website:

Note: Each box will be called the “phase” of the event cycle.

Each phase has a callback FIFO queue to execute. While each phase is special in its own way, typically, when an event loop enters a given phase, it performs any operation specific to that phase, and then performs callbacks in the queue for that phase until the queue is exhausted or the maximum number of callbacks has been executed. When the queue is exhausted or the callback limit is reached, the event cycle moves to the next phase, and so on. As the event cycle is a core point, it is often questioned by the interviewer about specific execution output. You can read my article

cross-platform

Initially, Node only ran on Linux platforms. As Node grew, Microsoft took note of its existence and put a team in place to help make Node compatible with Windows, and by the time v0.6.0 was released, Node was running directly on Windows. Node is cross-platform based on Libuv.

The disadvantages of the Node. Js

Disadvantages of single threading

One feature of Node.js is single threading, which brings many benefits, but it also has disadvantages. Single threading has the following weaknesses.

  1. Unable to utilize multi-core CPUS
  2. The error causes the entire application to exit and cannot continue to call asynchronyI/O
  3. A large amount of computation consumes the CPU and makes it impossible to continue the asynchronous callI/O

All of the above are drawbacks of Node, but there are some corresponding solutions:

Downside # 1: Solutions

  • (1) Some management tools such asPm2, foreverAnd so can achieve the creation of multi-process to solve the utilization of multi-core CPU.
  • (2) Before v0.8, the implementation of multi-process can be usedchild_process
  • (3) After V0.8, it can be usedclusterModule, through the master-slave mode, create more than one working process to solve the utilization problem of multi-core CPU.

Downside # 2: Solutions

  • (1) Nnigx reverse proxy, load balancing, open multiple processes, bind multiple ports;
  • (2) Some management tools such asPm2, foreverCan realize process monitoring, error automatic restart, etc
  • (3) Open multiple processes to listen on the same port, using Node providedclusterModule;
  • (4) Did not appearclusterBefore, you can also use itchild_processCreate multiple child threads to listen on a port.
  • (5) Note that the above solutions exist, but when writing node back-end code, an exception is throwntry catchAll the more necessary.

Downside 3: Solutions

  • (1) A large number of intensive computations can be divided into multiple sub-threads as above
  • However, if splitting is not allowed, Node is really helpless in a single thread to compute a million large pieces of data, which is itself a drawback of V8’s memory limitations.

Note: CHILD_process and cluster modules will be discussed in a separate article.

The good news is that with the release of Node and the new API module, these problems seem to have been solved.

debugging

For those of you who have used Node, the first thing you might think is that debugging is too hard. There is no stack trace, so debugging is difficult.

NPM package in the Node community

The Node.js community has a lot of packages of varying quality, and if you want to be lazy and happen to NPM a defective package, you’re in trouble because the code is open source and you have to debug it yourself.

Application scenarios of Node.js

This paper introduces the features and disadvantages of Node.js, and then discusses the application scenarios of Node.js.

What kind of applications is Node.js suitable for?

Good at I/O, bad at calculation. Because node.js is good at scheduling tasks, if your business has a lot of CPU computation, which is essentially blocking the single thread, it’s not a good fit for Node development, but it’s not without a solution, it’s just not a good fit.

Node.js is great for applications that need to handle a lot of concurrent I/O, but don’t need very complex processing inside the application before issuing a response to the client. Node.js is also ideal for working with WebSocket to develop long-connected, real-time interactive applications.

Specific scenarios can be shown as follows:

  • The first category: user form collection system, background management system, real-time interaction system, examination system, networking software, high concurrency Web applications;

  • The second category: web, Canvas and other multiplayer online games;

  • The third category: Web-based multi-person real-time chat client, chat room, graphic live broadcast;

  • Fourth category: single-page browser applications;

  • Fifth category: working with databases and providing JSON-based apis for front-end and mobile applications;

  • The sixth category,….

Which big companies are using it

  • Yahoo: Yahoo is openCooktailFramework,YUI3The capabilities of this front-end framework extend to the server side with Node.
  • Tencent: Node is applied to long connections to provide real-time functionality.
  • Petal net, Mushroom Street: Throughsocket.ioImplement real-time notification.
  • Ali: The main use is parallelismI/OThese capabilities, which enable efficient distribution, themselves come out of many Node frameworks
  • LinkedIn: Mobile sites are also used for Node
  • Netease: The game field has high requirements for concurrency and real-time. Netease has opened source the Real-time framework of Nodepomelo
  • And so on…

Reference article: Part of this article is from Teacher Piao Ling’s “Simple Node.js”

Hello everyone, I’m Koala, doing a node.js advanced route, today share so much, if you are interested in sharing content, you can follow the public account “Programmer growth point North”, or join the technical exchange group, we discuss together.

Join us and learn!

.