"Turn" "translate" what is node. js?

Source: Internet
Author: User

Copyright belongs to the author.
Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Director Jia
Links: https://www.zhihu.com/question/33578075/answer/56951771
Source: Know

If you've noticed technical news last year, I bet you see at least one or two node. js. So the question is, "What is node. js?" ”。 Some people will probably tell you, "This is something that is developing Web services through the JavaScript language." If this obscure explanation hasn't knocked you out, you might have asked, "Why are we using node. js?" "Others will tell you that node. JS has features such as non-blocking, event-driven I/O, which makes it possible to use highly concurrent (high concurrency) in polling (Polling) and comet-built applications.

When you read the explanations and feel the same as the heavenly book, you don't even bother to ask. But it's okay. My article is to help you understand node. js while avoiding high-end terminology.


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 an improvement. 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 rotation is a technique used to simulate persistent connections using HTTP. 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.

"Turn" "translate" what is node. js?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.