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

What can node.js do? A summary of the functions of node.js in a few minutes

寻∝梦
Release: 2018-09-05 14:11:04
Original
8930 people have browsed it

This article mainly introduces the definition of node.js, as well as a summary of the role of node.js. I hope everyone can learn more, let's read this article together

First, let’s take a look at what node.js can do?

This is a more formal introduction to node

Node.js is a JavaScript running environment based on the Chrome V8 engine.

Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient. (Event-driven: A strategy for decision-making during the event triggering process. Simply put, it is to follow the things that appear at the current point in time and call available resources to solve the things, so that things that continue to appear can be solved and prevent the accumulation of things)

Node.js’s package manager npm has become the world’s largest open source ecosystem. If you want to know more, come to PHP Chinese websitenode.js video tutorial

These are the definitions of nodejs, now look at the summary of the role of nodejs:

Node.js has several particularly significant advantages: fast, high performance, high development efficiency, and wide range of applications.

Others will usually tell you vaguely: node.js is non-blocking. Features such as event-driven I/O make it possible to build applications with high concurrency (Polling) and comet.

When you feel you still don’t quite understand after reading these explanations, I will help you understand node.js in a simple and crude way.

The process of the browser sending requests to the website has not changed much. When the browser sends a request to the website. The server receives the request and begins searching for the requested resource. If necessary, the server will also query the database and finally send the response results back to the browser.

However, in traditional web servers (such as Apache), each request will cause the server to create a new process to handle the request.

Later came Ajax. With Ajax, we don't have to request a complete new page every time. Instead, we only request part of the page information we need each time. This is obviously an improvement

. But if you want to build a social networking site like FriendFeed (a website similar to Renren to refresh your friends’ news), your friends will push new statuses at any time, and your news will be automatically refreshed in real time.

To achieve this requirement, we need to allow users to maintain an effective connection with the server. The simplest implementation method at present is to maintain long polling between the user and the server.

HTTP request is not a continuous connection. You request once, the server responds once, and then it’s over. Long polling is a technique that uses HTTP to simulate a persistent connection. Specifically, as long as the page is loaded, whether you need the server to respond to you or not, you will send an Ajax request to the server.

This request is different from the general Ajax request. The server will not directly return information to you, but it will wait until the server feels it is time to send you information, and then it will respond. For example, if your friend posts a new message, the server will send the new message to your browser as a response, and then your browser will refresh the page. After the browser receives the response and refreshes it, it sends a new request to the server. This request still will not be responded to immediately. So I started repeating the above steps. Using this method, the browser can always wait for a response. Although the above process still only involves non-persistent HTTP, we simulated a seemingly continuous connection state

Let's look at traditional servers (such as Apache). Every time a new user connects to your website, your server has to open a connection. Each connection requires a process, and these processes are idle most of the time (for example, waiting for your friend to send new news, waiting for the friend to finish sending the message before responding to the user. Or waiting for the database to return query results, etc.).

Although these processes are idle, they still occupy memory. This means that if the number of user connections increases to a certain scale, your server may run out of memory and collapse.

How to solve this situation? The solution is what was just mentioned above: non-blocking and event-driven. These concepts are actually not that difficult to understand in the scenario we are talking about.

You think of a non-blocking server as a loop, and this loop will keep running. When a new request comes, this loop receives the request, passes the request to other processes (such as a process that performs database queries), and then responds with a callback. When it's done, the loop will continue to run and receive other requests. Come down like this. The server will not wait for the database to return results as before.

If the database returns the result, the loop will return the result to the user's browser and continue running. This way, your server's processes won't be waiting idle. Therefore, in theory, there is no limit to the number of database queries and user requests at the same time. The server only responds when an event occurs on the user's side. This is event-driven.

FriendFeed uses the Python-based non-blocking framework Tornado (Zhihu also uses this framework) to implement the new news function mentioned above. However, Node.js is even better than the former.

Node.js applications are developed through javascript and then run directly on Google's abnormal V8 engine. With Node.js, you don't have to worry about the client's request running a piece of code in the server that can cause blocking. Because JavaScript itself is an event-driven scripting language. If you think about it, when you write JavaScript for the front end, you mostly deal with event processing and callback functions. JavaScript itself is a language tailored for event processing.

Node.js is still in its early stages. If you want to develop an application based on Node.js, you will probably need to write some very low-level code.

But the next generation of browsers will soon adopt WebSocket technology, and long polling will also disappear. In web development, technologies like Node.js are only going to become more and more important.

The above is the summary of this article about node.js. It is very detailed. I hope you can read more so that everyone can better understand node.js. Students who want to learn more can go to the PHP Chinese websiteNode.js Development ManualColumn

[Editor’s Recommendation]

How to customize fonts in css? Introduction to text font styles in html

#html How to use the base tag? Summary of usage of html base tag

The above is the detailed content of What can node.js do? A summary of the functions of node.js in a few minutes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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