Node.js since 2009, has been developed for more than two years, and its growth rate is obvious to all. From the amount of traffic in GitHub over rails, by the end of last year NODE.JSS founder Ryan Dalh joined Joyent to get corporate funding, and then to release the Windows transplant version this year, Node.js's prospects have been affirmed by the tech community. Infoq has been focusing on the development of Node.js, with special lectures at the two Qcon conferences (Beijing and Hangzhou stations) this year. In order to better promote the node.js in the domestic technology promotion, we decided to open "The Simple node.js" column, invited from the Node.js field evangelist, the developer, Technical experts to describe the various aspects of node.js, so that readers have a more in-depth understanding of node.js, and can actively engage in the discussion and practice of new technologies.
The first article of the column, "What is Node.js" tries to expound the basic concept, development history and advantages of node.js from various angles, and the developers who are unfamiliar with this field can learn some basic knowledge of node.js through this article.
Start with the name.
More and more technical reports about Node.js, Node.js writing is also a variety of written Nodejs, there are written Nodejs, in the end which one of the most standard of writing, we may wish to follow the official statement. On the official website of Node.js, it has always been called "node" or "Node.js", and no other statements have been found, "node" is the most used, considering the meaning of the word node and its use is too broad, it is easy for developers to misunderstand, we use the second name-" Node.js ", JS suffix point out the node project's original intention, the other names are diverse, there is no exact source, we do not recommend use.
Node.js is not JS application, but JS operating platform
See node.js This name, beginners may mistakenly think that this is a JavaScript application, in fact, Node.js is written in C + + language, is a JavaScript running environment. Why use C + + language? According to Node.js founder Ryan Dahl recalls, he originally wanted to use Ruby to write Node.js, but later found that the performance of Ruby virtual machine can not meet his requirements, and later he tried to use the V8 engine, so chose the C + + language. Since it is not JavaScript application, why is it called. js? Because Node.js is a JavaScript-running environment. When it comes to JavaScript, the first thing you think about is the daily use of browsers, and modern browsers contain a variety of components, including rendering engines, JavaScript engines, and so on, where the JavaScript engine is responsible for interpreting JavaScript code in the execution Web page. As one of the most important languages on the Web front-end, JavaScript has always been a patent for front-end engineers. However, Node.js is a back-end JavaScript runtime environment (supported systems including *nux, Windows), which means you can write system-level or server-side JavaScript code and give it to node.js to interpret execution, and simple commands like this:
#node helloworld.jsNode.js
Using the Google Chrome browser's V8 engine, the performance is very good, but also provides a lot of system-level APIs, such as file operations, network programming. Browser-side JavaScript code is run with a variety of security restrictions and limited operation on the client system. Node.js, by contrast, is a comprehensive background runtime that provides JavaScript with many of the features that other languages can achieve.
Node.js uses event-driven, asynchronous programming for network services and design
The word event driver is not unfamiliar, in some traditional language network programming, we will use the callback function, such as when the socket resource reached a certain state, the registered callback function will execute. Node.js's design idea is event driven, and the vast majority of the APIs it provides are event-based, asynchronous styles. Take the net module as an example, where the net. The socket object has the following events: Connect, data, end, timeout, drain, error, close, etc., developers who use node.js need to register the corresponding callback function according to their business logic. These callback functions are executed asynchronously, which means that although the functions appear to be registered sequentially in the code structure, they do not depend on the order in which they appear, but wait for the appropriate event to be triggered. Event-driven, asynchronous programming design (interested readers can access the author's other article, "Node.js Asynchronous Programming Style"), the important advantage is that the full use of system resources, executing code without blocking wait for some kind of operation completed, limited resources can be used for other tasks. This type of design is ideal for back-end network service programming, and Node.js's goal is to do so. Concurrent request processing is a big problem in server development, and blocking functions can lead to waste of resources and time delay. With event registration and asynchronous functions, developers can improve resource utilization and performance.
From the support modules provided by Node.js, we can see that many functions, including file operations, are executed asynchronously, which differs from traditional languages, and that for the convenience of server development, Node.js has a lot of network modules, including HTTP, DNS, NET, UDP, HTTPS, TLS and so on, developers can build a Web server quickly on this basis. Take the simple helloworld.js as an example:
var http = require (' http ');
Http.createserver (function (req, res) {
res.writehead ({' Content-type ': ' Text/plain '});
Res.end (' Hello world\n ');
Listen (80, "127.0.0.1");
The above code builds a simple HTTP server (run the sample deployed in http://helloworld.cnodejs.net/, readers can access), listen to 80 ports locally, and for any HTTP request, the server returns a header status code of 200, "Hello World" Content-type ' value is text/plain ' response. From this small example, we can see some points:
Node.js's network programming is more convenient, provided by the module (here is HTTP) Open the Easy-to-use API interface, a few lines of code can build the server.
It embodies event-driven, asynchronous programming, and specifies a callback function (implemented using JavaScript's anonymous function) in the parameters of the Createserver function, and when an HTTP request is sent over, Node.js calls the callback function to process the request and respond. Of course, this example is relatively simple, there are not too many event registration, in future articles readers will see more practical examples.
Characteristics of Node.js
Now let's talk about the characteristics of node.js. The characteristics of event-driven, asynchronous programming have just been said in detail, and this is not repeated here.
Node.js performance is good. Performance is an important factor in node.js considerations, according to founder Ryan Dahl, and choosing C + + and V8 instead of Ruby or other virtual machines is also based on performance. Node.js is also very bold in design, it runs in single process, single-threaded mode (very surprised, right?). This is consistent with the way JavaScript works), The event-driven mechanism is implemented by node.js to efficiently maintain an event loop queue through an internal single thread. There is no multi-threaded resource usage and context switching, which means that in the face of massive HTTP requests, Node.js takes care of everything with event drivers, and Web service developers who are accustomed to traditional languages may be familiar with multithreaded concurrency and collaboration , but in the face of node.js, we need to accept and understand its characteristics. So can we speculate that such a design would cause the load pressure to concentrate on the CPU (event loop processing?). Instead of memory (remember the days when Java virtual machines throw outofmemory exceptions?) , seeing is believing, better come to see Taobao shared data platform team's performance test for Node.js:
Physical machine configuration: RHEL 5.2, CPU 2.2GHz, Memory 4G
Node.js Scenario: memcache agent, Fetch 100 byte data at a time
Connection Pool Size: 50
Number of concurrent users: 100
Test results (Socket mode): Memory (30M), QPS (16700), CPU (95%)
From the results above, we can see that in such a test scenario, the QPS can reach 16,700 times, memory only occupy 30M (where the V8 heap occupies 22M), the CPU is 95%, may become a bottleneck. In addition, many practitioners have done a performance analysis of Node.js, in general, its performance is convincing, but also a popular important reason. Now that the node.js is a single process, single-threaded mode, how can a single core performance node.js take advantage of multi-core CPUs in today's multi-core hardware-popular environment? Founder Ryan Dahl recommends running multiple node.js processes and using certain communication mechanisms to coordinate tasks. At present, many Third-party Node.js support modules have been released, and the articles behind the column will detail the Node.js programming under multi-core CPUs.
Another feature of Node.js is that the programming language it supports is JavaScript. A comparison of the pros and cons of dynamic and static languages is no longer discussed here. Just say three points:
var hostrequest = http.request (requestoptions,function (response) {
var responsehtml = ';
Response.on (' Data ', function (chunk) {
responsehtml = responsehtml + chunk;
});
Response.on (' End ', function () {
console.log (responsehtml);
Do something useful
});
In the above code, we need to deal with the responsehtml variable in the end event, due to JavaScript's closure characteristics, we can define the responsehtml variable outside of the two callback functions, and then constantly modify its value in the callback function corresponding to the data event. And eventually access the processing in the end event.
JavaScript, as the main language of front-end engineers, has considerable appeal in the technical community. Moreover, with the continuous development of web technology, especially the importance of the front-end increased, many front-end engineers began to test water "background application", in many companies using node.js, engineers have said that because of the habit of JavaScript, so choose Node.js.
JavaScript anonymous functions and closure characteristics are very suitable for event-driven, asynchronous programming, from the HelloWorld example we can see that the callback function in the form of anonymous functions, very convenient. The closure is more useful, looking at the following code example:
JavaScript performs well in dynamic languages, with developers performing performance analysis on dynamic languages such as Javacript, Python, and Ruby, and discovering that JavaScript performance is better than other languages, plus the V8 engine is the same kind of leader, So Node.js's performance also benefits.
A brief history of Node.js development
In February 2009, Ryan Dahl on a blog announcing his intention to create a lightweight Web server based on V8 and provide a library.
In the May 2009, Ryan Dahl released an initial version of the Node.js package on GitHub, and in the months that followed, someone started using node.js to develop applications.
Node.js lectures were arranged at the two session of the JSCONF Congress in November 2009 and April 2010.
At the end of 2010, Node.js received joyent funding from cloud computing services, founder Ryan Dahl joined Joyent full-time responsible for node.js development.
In July 2011, Node.js released a version of Windows with Microsoft's support.
Node.js Application Case
Although Node.js was born just two years ago, but its development momentum gradually overtook Ruby/rails, we listed here some of the enterprise application Node.js case, listen to the voice from customers.
In the latest mobile apps released by social networking site LinkedIn, Nodejs is the background for the mobile app. Kiran Prasad, head of mobile development at LinkedIn, said to the media that the entire mobile software platform was built by Nodejs:
LinkedIn uses a lot of technology in-house, but on the mobile server, we're completely based on node.
(The reason for using it) is first, because of its flexibility. Second, if you understand node, you'll find that the thing that it's best at is communicating with other services. Mobile applications must interact with our platform APIs and databases. We didn't do too much data analysis. Compared to the previous Ruby on Rails technology, the development team found that node has a much better performance. They ran 15 virtual servers (15 instances) on each physical machine, 4 of which were able to handle double traffic. The capacity assessment is based on the results of the load test.
Enterprise Social Services Web site Yammer uses node to create a Cross-domain proxy server for its own platform that Third-party developers can use to implement AJAX communications from their own domain-managed JavaScript code and the Yammer platform API. Yammer platform Technology director Jim Patterson his views on the advantages and disadvantages of node:
Because node is event-driven and non-blocking, it is well suited to handle concurrent requests, so a proxy server built on node is much better than a server performed by other technology implementations, such as Ruby. In addition, client code that interacts with the node proxy server is written in the JavaScript language, so the client and server side are written in the same language, which is a wonderful thing.
Node is a relatively new open source project, so it's not stable, it's always changing and lacks enough third-party library support. It looks like the way Ruby/rails was.
The well-known project hosting website GitHub also tried node application. This node application, called Nodeload, is an archive download server (used whenever you download a tarball or zip file for a storage branch). The archive download server before GitHub is written in Ruby. In the old system, downloading the archived request creates a Resque task. The task actually runs a git archive command on the archiving server to fetch data from a file server. Then, the initial request assigns you a small Ruby Sinatra application to wait for the task. It's just checking to see if the Memcache flag exists, and then redirecting it to the final download address. The old system runs about 3 Sinatra instances and 3 Resque worker. GitHub developers think this is a good opportunity for node application. node is based on event-driven, and node is better able to handle git archiving than Ruby's blocking model. In the process of writing a new download server, the developer felt that node was the right fit for the feature, and in addition, they used the node library Socket.io to monitor the download status.
Not only in foreign countries, the advantages of node also attracted the attention of domestic developers, Taobao practical application of node technology:
MyFox is a data processing middleware that extracts data from a MySQL cluster, calculates and outputs statistical results. The user submits a section of SQL statement, MyFox according to the semantics of the SQL command, generate each database fragment need to execute the query statement, send to each slice concurrently, the result is summarized and calculated. MyFox is characterized by CPU-intensive, no file IO, and only the processing of read-only data. MyFox was originally written using PHP, but encountered many problems. For example, PHP is single-threaded, MySQL needs to block queries, so it is difficult to request data concurrently, the solution is to use Nginx and Dirzzle, and based on the HTTP protocol implementation interface, and through the Curl_multi_get command to request. However, the MyFox project team ultimately decided to use Node.js to implement MyFox.
There are many reasons to choose Node.js, such as taking interest and community development into account, while also hoping to increase concurrency and squeeze dry CPUs. For example, frequent opening and closing of a connection causes a large number of ports to wait, and when the number of concurrent numbers goes up, the connection will often fail because the port is not available (in Time_wait state). It is often done by modifying system settings to reduce the wait time to circumvent this error, but using a connection pool can be a good solution to this problem. In addition, previously MyFox would have a very intensive access pressure with some cache failures, and Node.js could share query state and allow some requests to "wait a moment" for the system to repopulate the cached content.
Summary
This paper briefly introduces the basic knowledge of node.js, including concept, characteristic, history, case and so on. As a platform of only 2 years old, Node.js's development momentum is obvious to all, more and more enterprises begin to pay attention to and try to node.js, the front and back end developers should understand the relevant content.