A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
The project is named Node.js, and developers are accustomed to calling it Node,node to provide a set of purely event-driven, non-blocking toolkits to build high concurrent applications.
Note: node allows you to simply build fast, scalable network services.
Since being introduced by Ryan Dahl, node has received extensive attention from the industry. They have started deploying fast and scalable network services with node. Node is really fascinating.
On the other hand because node is petite, node's core set of functions is small, and the existing APIs are very refined, minimizing complexity for developers. When you want to build some more complex applications, you can just pick and install some of your favorite third-party modules.
There's also a reason for node to be so appealing that it's easy to get started and you can download and install it and run it in a few minutes.
node is usually installed according to the steps on the official website (http://nodejs.org), which supports Windows,linux,macintosh and Solaris.
To install node on Windows
Node supports Windows from version 0.6.0, to install node on Windows, use only from Http://nodejs.org/#download下载node-v*.msi, and then double-click to run it. You may then encounter a security dialog box similar to Figure 1-1.
Click the "Run" button, and when the download is complete, another Security dialog box appears (Figure 1-2) to remind you if you are sure of the operation.
If you agree, the Node Installation Wizard will appear (Figure 1-3), click Next node will begin installation, wait a little while the installation is finished! See Figure 1-4
Install under Mac OS X
If you use Mac OS X, you can use the Setup Wizard to install node, first, go to http://nodejs.org/#download下载node-v*.pkg, after downloading, double-click to run, you will see the Installation Wizard's first dialog box, see Figure 1-5
Click "Continue" installation, then the wizard will ask you to enter the system user's password, after the installation will be determined to start, is a little while, node and installed! See Figure 1-6
Install with source code
If you use a UNIX system, you can install it by compiling the source code. First, you need to select the node version you want to install, then download the corresponding source code and build, install and run node.
Note: node relies on several Third-party code libraries, but fortunately most of them are already included in the node release package, and if you start building from the source, you need two things below:
1.python (more than 2.4 versions)--The build tools released with node require a Python environment to run
2.libssl-dev--If you're going to use SSL/TLS encryption, you need to install this, LIBSSL is the class library used by the OpenSSL tool, and you can usually install it using the system's Package manager under Linux and UNIX systems. Libssl is preinstalled under Mac OS X, so if you use a Mac OS X system you usually don't have to install LIBSSL.
Select node version
The official website nodejs.org usually has two different node versions to download: Stable and latest.
For node, the smallest bit of version number represents the stability of this version, the stable version uses even numbers (such as 0.2,0.4,0.6), and the unstable version uses odd (0.1,0.3,0.5,0.7).
Non-stable versions are not only functionally unstable, but the APIs are also likely to change in subsequent versions, and the published APIs in the stable version are not modified. For each stable branch, the new patches include not only bug fixes, but also changes to the APIs in the unstable version.
Unless you want to test the new features in the latest unstable version, you should choose the latest stable version. The unstable version is like a battlefield for the node core team to test the new features.
Although more and more projects and companies have successfully used node in their products (home page of the website), you may have to learn to endure the changes in the API from the unstable version to the stable version, which is, of course, the price of learning a new technology.
Download Node source code
Now you know which version to download, then go to the official website http://nodejs.org find the corresponding tar package, and then copy the download link, if you use the UNIX system, your system may have installed the wget, which means you can download with a shell command:
If you do not install wget, you may need to use curl:
If neither of your tools is installed, you'll have to find another way to download the TAR packets to your local directory--for example, through a browser or through a local network.
(Examples of this book use the latest stable version of writing: 0.6.12)
Now that we have the source code, we can use it to build the node executable file. First, you need to extract the previously downloaded tar packets:
Then enter the source directory:
When built, use the following command to install node:
If you encounter a permission problem, precede the command with Sudo, and execute it as the root user:
Now that you're ready to run node, you can simply experience node's command-line interface (Cli:command-line interface) and just call the node executable on the line:
Preparing and using the node Package Manager
So far, you can only use the language features and core functions of node itself, which is why most program platforms have a system for downloading, installing, and managing Third-party modules, and in node we use the Node Package Manager (Npm:node Package Manager)
NPM consists of three parts: a code base for storing Third-party packages, a mechanism for managing locally installed packages, and a standard for defining package dependencies. NPM provides a public registration service that contains all the packages that are published and provides a command-line tool for downloading, installing, and managing these packages. You can follow node's package format criteria to make your package or apply other Third-party packages that you need to rely on.
Although you can start using node without knowing NPM, you have to learn it if you want to use a Third-party package, because node itself provides only low-level APIs, and using Third-party modules can dramatically reduce development complexity without having to encode everything yourself. NPM allows you to download and use modules in a sandbox, and you can experiment with what you're interested in without worrying about polluting the global package environment.
NPM and node previously needed to be installed separately, and NPM has been included in the node installation package since version 0.6.0.
Use NPM to install, upgrade, and uninstall packages
NPM is very powerful and you can use it in a number of ways, and its code base centrally manages all of the public modules, and you can access it through http://search.npmjs.org. The author of the node open source module can publish its own module to NPM, and others can download it with the module name in the package installation description and install the module.
This section contains some common operations for installing and deleting packages, knowing this is enough to get you started managing the Third-party packages on which your application relies, however, you first need to understand how these commands differ in "global" and "local" mode, and how they affect dependencies and module lookups.
Global and local mode for NPM module
There are two main modes of NPM operations: Global and Local. Both modes affect the directory structure that the package holds, and the order in which node loads the package.
Local mode is the default mode of operation for NPM, in which NPM works only in the working directory and does not cause system-wide modifications, which allows you to install and test the modules in a node program without affecting other node programs on your computer.
Global schemas are suitable for public modules that will be used by many programs and are always loaded globally, such as command-line tools that are not used directly by the application.
If you don't know which mode a module should be installed in, then you should use local mode. If a module's author needs a module to be installed globally, he will usually point it out in the installation instructions.
If you use the default directory when you install node, NPM installs the package to/usr/local/lib/node_modules under Global mode. If you execute the following command, NPM will search for and download the latest version called sax and install it into the/usr/local/lib/node_modules/sax directory.
The default mode is local mode, so you need to add the-G tag after the NPM command to enable the global mode.
The local mode is the default recommended mode for the node package dependency mechanism, in which everything that NPM installs is in the current working directory (the root directory is no exception) without affecting any global settings. This mechanism allows you to set up your application dependencies and their versions one at a a while without worrying about polluting the global module space. This means that you can have two applications that rely on different versions of the same module, but they don't create conflicts.
In this mode, NPM uses the Node_modules directory in the current working directory to store the module, such as your current working directory is/HOME/USER/APPS/MY_APP,NPM will be used/home/user/apps/my_app/node_ Modules to store all the local modules. This means that if you use the module name to refer to the module in your code, node will first look in the local node_modules directory, and if it is not found, it will search the global Node_modules directory, and the local module priority is always higher than the global module.
Use the following command to install the latest version of a module:
For example, to download and install the latest version of a module named sax, you first need to set your application's root directory to the current directory, and then enter:
You can also choose to install a specific version by using the following command:
Use the following command to uninstall a local module:
Use the following command to update the local module:
Working with executable files
A module can contain one or more executable files, and if you use the default directory settings to install a global module, NPM installs the executable file into the/usr/local/bin directory, which is usually set as part of the system PATH environment variable. If you install this module locally, NPM will put all executables into the./node_modules/.bin directory.
NPM installs not only the module packages you need, but also the other modules on which they depend, for example, if you need to install module A and a is dependent on modules B and C, then B and C will be installed in the./node_modules/a/node_modules directory when you install a.
For example, you install a module called Nano locally with the following command:
This tells you that the Nano module relies on underscore and request two modules, and also points to the installed version. If you look at the./node_modules/nano/node_modules directory now, you will find that these two modules have been installed:
Defining dependencies using the Package.json file
When you start writing an application, you can create an Package.json file in the application root to define the application's metadata, such as the name of the application, the author, the code base address, the contact information, and so on. The external modules that the program relies on are also specified in this file.
If you don't want to publish the program to NPM, you don't have to build the file, but even if your program is private, the file is actually useful, and it tells NPM about the application's dependencies. (Translator Note: For example, you copy project source code from the development environment to the production environment, you can install all the dependent packages at once by calling NPM install. NPM will automatically complete the download installation of the dependent modules through the dependencies specified in the Package.json, without having to operate them one at a while.
Package.json is a JSON-formatted file that contains a list of attributes, but only one dependencies attribute is used to illustrate the dependencies of the program. For example, an application called MyApp relies on the Sax,nano and request modules and only needs to establish such a Package.json:
You specify a MyApp application that relies on the 0.3 version of Sax, any version of the Nano, and the request module with a version higher than 0.2.0.
Note: You may find that if you specify the name and version fields, NPM will not work, and this will only happen in the old version of NPM, since NPM was originally used for public modules, not private programs.
Then, in the root directory of the application, execute:
In this way, NPM analyzes dependencies and your local node_modules directory, and automatically downloads and installs missing modules.
You can also update all local modules to the latest version of your defined dependency settings with the following command:
This chapter has learned how to install node and Node Package Manager (NPM), and you can now use NPM to install, uninstall, remove any third-party modules, and learn how to work with Package.json files to use NPM to manage application dependencies.
Now that you have node and NPM installed, you can try it out, but first you need to know a bit about node and event-driven knowledge, which will be covered in the next chapter.
Start building with 50+ products and up to 12 months usage for Elastic Compute Service