Home > Web Front-end > JS Tutorial > body text

Node.js Getting Started Tutorial

巴扎黑
Release: 2017-08-22 17:01:19
Original
1467 people have browsed it

Node.js is a platform built on the Chrome JavaScript runtime. Next, I will share with you the prequel to getting started with node.js through this article. Friends who are interested should take a look together

1. What is NodeJS

Simply put, Node.js is JavaScript running on the server.

Node.js is a platform built on the Chrome JavaScript runtime.

Node.js is an event-driven I/O server-side JavaScript environment, based on Google's V8 engine (the JavaScript execution environment used by Google's Chrome browser),

The V8 engine executes Javascript Very fast and very good performance.

2. Why choose NodeJS

If you are a front-end programmer and you don’t know dynamic programming languages ​​like PHP, Python or Ruby , and then you want to create your own service, then Node.js is a very good choice.

Node.js is JavaScript running on the server side. If you are familiar with Javascript, then you will easily learn Node.js.

Of course, if you are a back-end programmer and want to deploy some high-performance services, then learning Node.js is also a very good choice.

3. Characteristics of NodeJS

Let’s first take a look at the introduction on the NodeJS official website:

Node.js is a platform built on 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 efficient, perfect for data-intensive real-time applications that run across distributed devices.

 Its characteristics are:

 1. It is a Javascript running environment

 2. Depends on Chrome V8 engine for code interpretation

 3. Event-driven

 4. Non-blocking I/O

 5. Lightweight, scalable, suitable for real-time data interaction applications

 6. Single process, single thread

Asynchronous, event-driven model

We also need to initiate a request and wait for the server-side response; but what is different from the bank example is that this time we got a number after ordering the meal.

After getting the number, we often We will wait at our location, and the requests behind us will continue to be processed. We also take a number and wait aside, and the receptionist can continue to process it.

When the meal is ordered, the number will be called. We get our own meal and carry out subsequent processing (eating)

This action of calling the number is called callback in NodeJS. It can continue to execute the subsequent logic (eating) after the event (cooking, I/O) is completed,

This reflects the salient features of NodeJS, asynchronous mechanism, event-driven

The entire process There is no need to block the connection of new users (ordering food), and there is no need to maintain the connection between users who have already ordered food and the chef

Node.Js uses an event-driven model. When the web server receives a request, it closes it and then Process it and then serve the next web request. When the request is completed, it is put back into the processing queue, and when the head of the queue is reached, the result is returned to the user. This model is very efficient and scalable because the webserver always accepts requests without waiting for any read or write operations. (This is also called non-blocking IO or event-driven IO)

Based on this mechanism, in theory, NodeJS can respond to users who request connections one after another, so NodeJS can support more than Java and PHP programs. Higher concurrency

Although maintaining the event queue also requires costs, and since NodeJS is single-threaded, the longer the event queue, the longer it takes to get a response, and the concurrency will still be insufficient

Summarize how NodeJS solves the problem of concurrent connections:

Change the way to connect to the server, and each connection emits (emit) a NodeJS engine process Events (Events) running in the event queue are put into the event queue,

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

I/O blocking

Another problem that NodeJS solves is I/O blocking. Look at this business scenario: you need to pull data from multiple data sources and then perform Processing

 (1) Serial acquisition of data, this is our general solution, take PHP as an example

If obtaining profile and timeline operations require separate 1S, then serial acquisition requires 2S

 (2) NodeJS non-blocking I/O, emitting/listening events to control the execution process

When NodeJS encounters an I/O event, it will create a thread to execute, and then the main thread will continue to execute.

Therefore, the action of taking the profile triggers a In an I/O event, the action of getting the timeline will be executed immediately.

The two actions are executed in parallel. If each takes 1S, then the total time is 1S

Their I/O operations After the execution is completed, an event, profile and timeline are emitted,

After the event agent receives it, it continues to execute the following logic. This is the feature of NodeJS non-blocking I/O

To summarize:

Java and PHP also have ways to implement parallel requests (sub-threads), but NodeJS can do it very naturally through callback functions (Callback) and asynchronous mechanisms

4. NodeJS Advantages and disadvantages

Advantages:

1. High concurrency (the most important advantage)

2. Suitable for I/O-intensive applications

Disadvantages:

1. Not suitable for CPU-intensive applications; the main challenges that CPU-intensive applications bring to Node are: due to the single thread of JavaScript, if there are long-running calculations (such as a large loop) will cause the CPU time slice to be unable to be released, making subsequent I/O unable to be initiated;

Solution: Decompose large computing tasks into multiple small tasks so that the computing can be released in a timely manner without blocking Initiation of I/O calls;

 2. Only supports single-core CPU and cannot fully utilize the CPU

 3. Low reliability. Once a certain link of the code crashes, the entire system will collapse

Reason: single process, single thread

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

) Open multiple processes to listen to the same port and use the cluster module;

4. The quality of the open source component library is uneven, updated quickly, and backwards incompatible

5. Debugging is inconvenient and wrong No stack trace

The above is the detailed content of Node.js Getting Started Tutorial. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template