What is Nodejs used for?

Source: Internet
Author: User

Some people say "this is something that develops Web services through the JavaScript language." More straightforward can be understood as: node. JS has non-resistive SE plug, event-driven/O-like features, which makes high concurrency (hi concurrency) in the polling and comet-built applications possible.

The browser's request to the website has not changed much. When the browser sends a request to the Web site, the server receives the request and begins 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 the Apache server, each request will cause the server to create a new process to handle the request.

And then the Ajax. With Ajax, we don't have to ask for a full new page every time, instead of just asking for the part we need every time. 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 on a valid link with the server. The simplest implementation now is to keep long polling between the user and the server (long polling).

The HTTP request is not a persistent link, 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 or, as long as the page is loaded, you will send an AJAX request to the server, regardless of whether you need the server to give you the appropriate information. 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 respond 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 needs to occupy a process, these processes are busy most of the time (for example, waiting for your friends to send new news, and so the friend sent to the user to respond to the message. 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 increases to a certain regularity, your server will probably consume light memory directly paralyzed.

How to solve this situation? The workaround 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. Think of the non-blocking server as a loop loop, which will run down one loop. 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). I'm done with this loop and I'm going to run and pick up the other requests. This way down, the server will not be as silly as before waiting for the database to return results.

If the database returns the results, loop passes the results back to the user's browser. Then continue to run. In this way, your server's process 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 something happens to the user, 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. But Nodejs is better than the former. Nodejs's application was developed through JavaScript, and then ran directly on Google's perverted V8 engine. With Nodejs, you don't have to worry about the client's request 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 back-up functions more often. JavaScript itself is a language tailored to event processing.

What is Nodejs used for?

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.