At present, node. JS is very popular in front page development, it is a set of JavaScript toolkit to write high-performance Web server, in this article, will lead you beginners to introduce the basic knowledge of Node JS, This article is required to read for readers with certain JavaScript and other development languages.
What is node. js
Node is a JavaScript run environment (runtime). It actually encapsulates the GOOGLEV8 engine (applied to Google Chrome). The V8 engine executes JavaScript very fast and performs very well. Node optimizes some of the special use cases, providing an alternative API that allows V8 to run better in a non-browser environment. For example, in a server environment, it is often necessary to process binary data, but JavaScript has insufficient support for this, so V8. Node adds the buffer class for easy and efficient processing of binary data. As a result, node not only uses V8, but also optimizes it to make it more resilient in all environments.
Development history
In February 2009, Ryan Dahl announced on his blog that he was prepared to create a lightweight Web server based on V8 and provide a set of libraries.
In May 2009, Ryan Dahl released the original version of the node. js package on GitHub, and in the next few months someone started using node. js to develop the app.
In November 2009 and April 2010, the two session of the JSCONF conference was organized with node. JS Lectures.
At the end of 2010, node. JS received joyent funding from cloud computing service provider, founder Ryan Dahl joined Joyent full-time responsible for node. JS Development.
In July 2011, node. JS released the Windows version with Microsoft Support.
First knowledge of node. js
To get started with node. JS, you must first understand the differences between node. JS and the traditional server-side scripting environment (such as: Php,python and Ruby, and so on).
It is believed that most readers are already familiar with Ajax programming, where a (asynchronous) in Ajax refers to asynchronous weaving, So in the node. JS World, all methods are executed asynchronously, so for example if you need to read a file in an operating system, you must specify a callback method, then the contents of this callback method will be executed after the file is read.
Below, a small contrast between node. JS and other languages allows the reader to see the clues. Because node. JS is not a server runtime environment, it is actually a JavaScript that runs on the service side and is therefore highly efficient in terms of performance. Here is a small program code written by node. JS:
varI, A, B, C, Max;max= 1000000000;varD =Date.now (); for(i = 0; i < Max; i++) {a= 1234 + 5678 +i; b= 1234 * 5678 +i; C= 1234/2 +i;} And the same PHP code is as follows: $a=NULL; $b=NULL; $c=NULL; $i=NULL; $max= 1000000000; $start= Microtime (true); for($i = 0; $i < $max; $i + +) {$a= 1234 + 5678 +$i; $b= 1234 * 5678 +$i; $c= 1234/2 +$i;} Var_dump (Microtime (true)-$start);
The following table is a performance list of the two comparisons, in microseconds, as follows:
The two applications are executed at the command line, so there is no server-side deferred execution. Each application was tested 10 times, averaged, and the results were obtained. If the number of cycles is relatively small, then PHP speed, but this advantage, with the increase in the number of iterations to reduce, you will find more than Nodejs slow!
You can see that node. js speed is fast, but it must be used correctly. Here are a few important concepts in node. JS:
Modules Module
Node. JS uses module modules to classify different functions to simplify application development. The modules module is somewhat like a class library in C language. Each node. JS class Library contains a very rich variety of functions, such as the HTTP module contains a lot of functions related to the HTTP function, can help developers to easily http,tcp/udp, such as the operation, but also can easily create HTTP and tcp/ UDP server.
It is very convenient to use modules in your program, just as follows:
Here, the HTTP class library is introduced, and references to the HTTP class library are stored in the HTTP variable. At this point, node. JS will search our app for the existence of a node_modules directory and search the directory for the presence of HTTP modules. If node. js cannot find this directory, it will go to the global module cache and the user can set the module's location by relative or absolute path, for example:
var mymodule = require ('./mymodule.js ');
The module contains a lot of functional code snippets, the code in the module is mostly private, meaning that the function methods and variables defined in the module can only be called in the same module. Of course, some methods and variables can be exposed to the module, this time you can use export is the object to implement, such as the following code:
var Pi =function (r) {return PI * R *function (r) { Return 2 * PI * r;};
In the above code, the variable pi is created, which can only be accessed in the same module, and the two method area and circumference are exposed externally with exports, so they can be accessed in other modules.
Try to avoid using global variables
node. JS is the V8 JavaScript engine that uses Google. Therefore, we should follow the most?? Practice to develop the client. For example, we should try to avoid putting everything in global variables. However, this is not always possible. In node. js, you can even omit the var keyword if you want to declare a global variable, so you can easily create a function of a global variable, such as:
globalvariable = 1function () {...};
But as far as possible, there should be fewer global variables to be used everywhere in node. js.
node. JS Installation
The following describes the installation of node. JS, first on the nodejs.org Web site to download the relevant installation package according to the operating system, for Linux installation, the following can be done:
sudo apt-get update sudo apt-get install node
Or:
sudo aptitude update sudo aptitude install node
node. JS will be in the SID Library and can now be added to the list of sources sources
sudo echo deb Http://ftp.us.debian.org/debian/sid main >/etc/apt/sources.list.d/sid.list
Note, however, that after the installation is complete, the/etc/apt/sources.list.d/sid.list needs to be removed.
For more information about node. js, or click here to read the original
node. js Introduction (GO)