[Study notes] basic introduction to node.js

Basic concepts of Node.js

Node.js is a Javascript toolkit forwriting high-performance web servers(use JS to develop server-side programs).

Features: single thread, asynchronous, event-driven;

Take an example of how the Apache server runs. For example, there are 150 threads in the thread pool. When a large amount of concurrency occurs (a large number of visitors flood in to access the server), it will go to the thread pool to fetch threads. The operation may be reading the library or writing, etc. After the thread is executed and the result is returned to the client, the thread will continue to be returned to the thread pool. At this time, the thread can be used by others.

So Node.js is single-threaded, how to provide services? At this time, another feature of "asynchronous" was introduced. For example, if the user's operation is to read the library, then a separate memory area is opened for him, and the other user is for writing, and then a separate memory area is opened for him. This operation It is equivalent to the concept of "triage" in the hospital.

This method is very fast, but at the same time it consumes a lot of memory . There is a million-level concurrent test on the Internet. If it is not optimized, 1M connection consumes 16G of memory.

Node.js compared to PHP

advantage

Disadvantage

High performance (mechanism problem)

Few users

High development efficiency (optimization aspect)

Less middleware

Wider application range (desktop system can be developed, Electron framework)

IDE is not perfect

 

Node.js current disadvantages and solutions:

  1. Multi-core is not supported by default, but it can be solved with Cluster;
  2. Server cluster is not supported by default, but it can be solved with Node-Http-Proxy;
  3. Use Nginx for load balancing, static processing by Nginx, dynamic processing by Node.js;
  4. Forever or Node-cluster can be used to achieve disaster recovery;

 

Common frame choices:

Sails based on Express, koa, Hapi, and Express.

  1. Express: The most used, complete, stable, complete documentation, large community;
  2. Koa: relatively advanced, poor stability;
  3. Hapi: More complicated, not suitable for beginners, suitable for large and complex projects;

 

Node.js installation test

Download link: http://nodejs.cn/

Here we test and download the 64-bit version of the Windows installation package (.msi).

Continue to the next step until it is completed.

Then we test whether the installation is successful, open CMD, and type "npm".

It can be found that the currently installed NPM version is "6.14.8", and the NODE version is "14.9.0".

The test outputs "Hello, World".

 

Hold down the "Shift" key, right-click the mouse, and select "Open command window here".

 

 

 

 

 

Guess you like

Origin blog.csdn.net/zhongguomao/article/details/108350278