This article mainly introduces the definition of node. js, as well as a summary of the role of node. js, I hope you learn more, let's come and see this article
First, let's look at what node. js can do.
This is node's more formal introduction.
node. JS is a JAVASCRIPT runtime environment based on the Chrome V8 engine.
node. JS uses an event-driven, non-blocking I/O model to make it lightweight and efficient. (Event-driven: a strategy for making decisions during event triggering, simply by following things that appear at the current point in time, invoking the available resources to resolve the thing, making the things that are constantly appearing, and preventing things from piling up)
The node. JS Package Manager NPM becomes the world's largest open source ecosystem. Want to know more come on topic.alibabacloud.comnode.js video tutorial
These are all about the definition of Nodejs, and now look at the summary of Nodejs's role:
node. JS has several distinct advantages: fast, high performance, high development efficiency, wide application range
Others will tell you vaguely: node. JS has features such as non-blocking, event-driven I/O, which makes it possible to use highly concurrent (high concurrency) polling (Polling) and comet-built applications.
When you read these explanations and feel that you still don't understand them, here's a simple and rude way to help you understand your understanding of node. js.
The browser's request to the website has not changed much. When the browser sends a request to the website. The server receives the request and then starts to search for the requested resource. If necessary, the server also queries the database and finally passes the response back to the browser.
However, in a traditional Web server (such as Apache), each request will cause the server to create a new process to handle the request.
Then there was Ajax. With Ajax, we don't have to ask for a full new page every time, instead, we just need to request some of the page information. This is obviously a progressive
。 But for example, if you want to build a social network such as FriendFeed (like Renren), your friends will push the new status at any time, and your news will automatically refresh in real time.
To achieve this, we need to keep the user connected to the server in a valid way. The simplest way to achieve this is to keep long polling between the user and the server (long polling).
The HTTP request is not a persistent connection, you request it once, the server responds once, and then it is done. Long polling is a technique for using HTTP to simulate persistent connections. Specifically, you will send an AJAX request to the server whenever the page is loaded, whether you need to respond to the server or not.
This request differs from the general Ajax request, and the server does not give you a direct return message, but it waits until the server feels that it should send you a message before it responds. For example, if your friend sends a new message, the server will send you a new response to your browser, and your browser will refresh the page. When the browser receives a response refreshed and then sends a new request to the server, the request is still not immediately answered. So you start repeating the above steps. This method allows the browser to always remain in a state of waiting for a response. While the above process is still only non-persistent HTTP engagement, we have simulated a seemingly persistent connection state
Let's look at traditional servers (like Apache). Every time a new user connects to your website, your server has to open a connection. Each connection requires a process that is idle for most of the time (for example, waiting for your friend to send something new, and then sending the message to the user when the friend is done.) Or wait for the database to return the query results or whatever).
Although these processes are idle, they still consume memory. This means that if the number of user connections grows to a certain size, your server will probably need to consume light memory directly paralyzed.
How to solve this situation? The solution is just above: non-blocking and event-driven. These concepts are not so difficult to understand in the context of what we are talking about.
You think of a non-blocking server as a loop loop, and this loop will keep running. A new request comes in, and this loop responds to this request, passing the request to other processes (such as to a database query process), and then responding to a callback (callback). After the loop is over, run and pick up the other requests. So down. The server will not be as foolish as before to wait for the database to return results.
If the database returns the results, loop passes the results back to the user's browser and continues running. In this way, the process of your server will not be idle waiting. In theory, there is no limit to the number of database queries at the same time, as well as the number of requests from users. The server responds only when there are events on the user's side, which is event-driven.
FriendFeed is a python-based non-blocking framework tornado (which is known to also use this framework) to implement the new features described above. However, node. JS is better than the former.
The app for node. JS is developed through JavaScript and runs directly on Google's perverted V8 engine. With node. js, you don't have to worry about client requests running a block of code on the server. Because JavaScript itself is an event-driven scripting language. You recall that when you write JavaScript to the front end, you are doing event handling and callback functions more often. JavaScript itself is a language tailored to event processing.
node. JS is still at an early stage. If you want to develop a node. JS-based application, you would need to write some very low-level code.
But the next generation of browsers will soon be using websocket technology, so long polling disappears. In web development, this type of technology for node. JS only becomes more and more important.
The above is the summary of the node. js, very detailed, I hope to see more, so that everyone better understanding of node. js. Want to learn more students can go to the Topic.alibabacloud.comNode.js Development manual column