Node.js simple entry prequel

1. What is nodejs

Simple node.js is JavaScript running on the server.

Node.js is a platform established based on Chrome JavaScript runtime.

node.js is an event-driven I / O server JavaScript environment, Google-based V8 engine (JavaScript execution environment used by Google’s Chrome),

V8 engine executes JavaScript The speed is very fast and the performance is very good.

2. Why choose NodeJS

If you are a front-end programmer, you don’t know how to perform dynamic programming languages ​​such as PHP, Python or Ruby, then you want to create yourself Services, then node.js is a very good choice. Node.js is JavaScript running on the server if you are familiar with JavaScript, then you will be easy to learn node.js. Of course, if you are a backend programmer, you want to deploy some high-performance services, then learn node.js is also a very good choice.

Let’s take a look at the introduction of NodeJS official website: Node.js is a platage, chrome’s JavaScript Runtime for Easily Building Fast, Scalable Network Applications. Node.js Uses An Event-Driven, Non-Blocking I / O Model That Makes It Lightweight and Effect, Perfect FoR Data-Intensive Real-Time Applications That Run Across Distributed Devices.

1. It is a JavaScript operating environment

2. Depending on the Chrome V8 engine for code interpretation

3. Event driver

4. Non-blocking I / O

5. Light, scalable, suitable for real-time Data Interaction Application

6. Single process, single-thread

Asynchronous, event drive model

We also It is to initiate a request, wait for the server to respond; but is different from the bank example, this time we have done a number,

get the number, we tend to wait at position, and The request we will continue to be processed, and the same is to wait for a number and then wait for the reception. Node.js简单入门前传

Waiting to the meal, will call the number, we got your own meals, follow-up treatment (eating)

This shouting number is called a callback in NodeJS (Callback), Can continue to perform the logic (eating) after the completion of the event (burning, I / O),

This reflects the significant characteristics of NodeJS, asynchronous mechanism, event driver

There is no connection to the new user’s connection (order), and it is not necessary to maintain the user who has already met the prince’s connection

node.js Using the event drive model, when the web server receives the request, turn it off and then Treatment, then go to the next web request. When this request is completed, it is put back to the process queue, and the result is returned to the user when the queue begins. This model is very efficient and scalability.Because WebServer has been subjected to requests and not waiting for any read and write operations. (This is also called non-blocking IO or event driver IO)

Based on such a mechanism, theoretically there is a user request connection, and NodeJS can respond, so NodeJS can support than Java, PHP program. Higher concurrency

Although the maintenance event queue also needs cost, then because Nodejs is a single thread, the longer the event queue, the longer the response time, and the hair is still not from the heart

Summary how nodejs solves concurrent connection with this problem:

Method to connect to the server, each connection transmit (EMIT) an event running in the NodeJS engine process (Event ), Put in the event queue,

instead of generating a new OS thread for each connection (and assigns some supporting memory)

I / O block

Another problem solved by NodeJS is I / O block, see such a business scene: you need to pull data from multiple data sources, then process

(1) Serial Get data, this is our general solution, as an example

, if you need 1s, serial acquisition requires 2s

(2) NodeJS Non-blocking I / O, transmit / monitor events to control execution process

Node.js简单入门前传

NodeJS encounters I / O events creates a thread to execute, Then the main thread will continue to execute down,

Therefore, take the action of PROFILE to trigger an I / O event, immediately perform the action of Timeline,

Node.js简单入门前传 two action parallel execution, If you need 1s, then the total time is 1S

After their I / O operation is executed, an event is transmitted.Profile and Timeline,

After the event proxy continues to perform the logic of the following logic, this is the characteristics of NodeJS non-blocking I / O

Summary:

Java, PHP There is also a way to implement parallel requests (sub-threads), but NodeJs will be very natural through callback function (Callback) and asynchronous mechanism

4. NodeJS advantages and disadvantages

Advantages:

1. High concurrent (most important advantage)
2. Suitable for I / O intensive application

Disadvantages:

1. Not suitable for CPU intensive applications; the challenges brought by the CPU-intensive application are mainly: due to the JavaScript single thread, if there is a long run calculation (such as a large loop), the CPU time film can not be released , Make subsequent I / O could not be initiated;

Solution: Decompose large arithmetic tasks as multiple small tasks, so that the operation can be released in a timely manner, no blocking I / O call;

2 Only the single-core CPU is supported, and the CPU

3. reliability is low, once the code is collapsed, the entire system crashes

Reason: single process, single thread

Solution: (1) Nnigx reverse proxy, load balancing, open multiple processes, bind multiple ports;

(2) Open a plurality of processes to listen to the same port, use cluster modules;

Open source component library quality is uneven, updated quickly, not compatible down

5. Debug is inconvenient, error is not STACK TRACE

summarize

The above is the Node.js introduced by Xiaobian to introduce the prequet introduction, I hope to help everyone.If everyone has any questions, please leave a message, the small package will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment