At the 2009 JSCONf conference, a young programmer named Ryan Dahl showed people a project he was working on, a JavaScript running platform based on Google V8 engine, which provides a set of event loops and low IO application programming interfaces (APIs). Unlike other server-side platforms, JavaScript is born to be event-driven IO, and this project greatly reduces the complexity of writing event-driven applications, so it quickly grew and became popular at an incredible speed and applied to actual projects. (Jack: This translation is not very reliable, original text: This project was not like other server-side JavaScript platforms where all the I/O primitives were event-driven and there was no way around it.)
This project is named Node.js, which developers are used to calling it Node. Node provides a purely event-driven non-blocking toolkit to build highly concurrent applications.
Note: Node allows you to easily build fast and scalable network services.
Since being introduced by Ryan Dahl, Node has attracted widespread attention from the industry. They have started deploying fast and scalable network services with Node. Node is so attractive.
On the one hand, because of JavaScript, JavaScript is the most widely used programming language on this planet. Most Web programmers have used JavaScript on the browser side, and the server side is a very natural extension.
On the other hand, because Node is petite and cute, Node's core function set is very small, and the existing APIs are very refined, minimizing complexity for developers. When you want to build more complex applications, you just need to choose and install some third-party modules you like.
There is also a reason why Node is so attractive, it is easy to get started, you can download and install it in minutes and run it.
Usually, you can install Node according to the steps on the official website (http://nodejs.org), which supports Windows, Linux, Macintosh and Solaris.
Install Node on Windows
Node supports Windows since version 0.6.0. To install Node on Windows, just download node-v*.msi from Http://nodejs.org/#download, and then double-click to run. Then you may encounter a security dialog similar to Figure 1-1.
Figure 1-1
Click the "Run" button, and after the download is completed, another security dialog box will appear (Figure 1-2), reminding you whether you are sure of the operation.
Figure 1-2
If you agree, the Node installation wizard will appear (Figure 1-3). Click Next to start Node installation, and the installation will be completed in a short time! See Figure 1-4
Figure 1-3
Figure 1-4
Install on Mac OS X
If you are using Mac OS X, you can use the installation wizard to install Node. First, go to http://nodejs.org/#download to download node-v*.pkg. After downloading, double-click to run. You will see the first dialog box of the installation wizard, see Figure 1-5
Figure 1-5
Click "Continue" to install, and the wizard will ask you to enter the password of the system user. After confirming, the installation will begin. After a while, Node is installed again! See Figure 1-6
Figure 1-6
Install with source code
If you use 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 it, install and run Node.
Note: Node relies on several third-party code bases, but luckily most of them are already included in the Node release package. If you start building from the source code, you need the following two things:
1.python (version 2.4 or above) - The build tool released with Node requires a python environment to run
2.libssl-dev - If you plan to use SSL/TLS encryption, you need to install this. libssl is a class library used by the openssl tool. In Linux and UNIX systems, you can usually use the system's package manager to install it. libssl is pre-installed under Mac OS X, so if you use Mac OS X, you usually don't need to install libssl anymore.
Select Node version
There are usually two different Node versions to download on the official website nodejs.org: the stable version and the latest version.
For Node, the minimum version number bit represents the stability of this version, the stable version uses even numbers (such as 0.2, 0.4, 0.6), and the non-stable version uses odd numbers (0.1, 0.3, 0.5, 0.7).
Not only is the non-stable version functionally unstable, but the API may also be changed in subsequent versions, and the APIs released in the stable version will not be modified. For each stable branch, the new patch not only includes bug fixes, but also changes to the API in the nonstable version.
Unless you want to test new features in the latest non-stable version, you should choose the latest stable version. The non-stable version is like a battlefield for the Node core team to test new features.
Although more and more projects and companies have successfully used Node in their products (the official website homepage is displayed), you may have to learn to endure the changes in the API from non-stable version to stable version. Of course, this is 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 to find the corresponding tar package, and then copy the download link. If you are using the UNIX system, your system may have wget installed, which means you can download it with a shell command:
The code copy is as follows:
$ wget http://nodejs.org/dist/v0.6.1/node-v0.6.12.tar.gz
If you don't have wget installed, you may need to use curl:
The code copy is as follows:
$ curl O http://nodejs.org/dist/v0.6.1/node-v0.6.12.tar.gz
If you have neither of these tools installed, you have to find other ways to download the tar package to your local directory - such as through a browser or through a local network.
(The latest stable version of the examples in this book when writing: 0.6.12)
Build Node
Now that we have source code, we can use it to build the executable file of Node. First, you need to unzip the tar package you downloaded earlier:
The code copy is as follows:
$tar xfz node-v0.6.12.tar.gz
Then enter the source directory:
Copy the code as follows: $ cd node-v0.6.12
Configuration:
Copy the code as follows: $./configure
If it goes well, you will see the prompts for success:
Copy the code as follows: 'configure' finished successfully (9.278s)
Then you can start compiling:
The code copy is as follows:
$ make
After compiling, there will be the following prompt:
The code copy is as follows:
build' finished successfully (0.734s)
Install Node
When built, use the following command to install Node:
Copy the code as follows: $ make install
This operation will copy the Node executable file to /user/local/bin/node
If you encounter permission problems, add sudo before the command and execute it as root user:
The code copy is as follows:
$ sudo make install
Run Node
Now that Node is running, you can simply experience Node's command line interaction interface (CLI: command-line interface), just call the Node executable file:
The code copy is as follows:
$ node
This operation will start the Node's command line interaction interface and wait for your input. Enter the following command to let Node do something:
The code copy is as follows:
> console.log('Hello World!');
Hello World!
> undefined
You can also run a JavaScript script file. For example, you create a file called hello_world.js and contains the following content:
The code copy is as follows:
console.log('Hello World!');
Then use the file name of this script as the first parameter parameter to call the Node executable file:
The code copy is as follows:
$ node hello_world.js
Hello World!
Finally, use Ctrl+D or Ctrl+C to exit the Node command line interactive interface.
Prepare and use 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. In Node, we use the Node Package Manager (NPM: Node Package Manager)
NPM contains three parts: a code base for storing third-party packages, a mechanism for managing local installed packages, and a standard for defining package dependencies. NPM provides a public registration service that contains all packages published by everyone and provides a command line tool to download, install and manage these packages. You can formulate other third-party packages that your package or application needs to rely on according to Node's package format standards.
Although you can start using Node without understanding NPM, if you want to use a third-party package, you must learn it, because Node itself only provides some low-level APIs. Using third-party modules can greatly reduce the complexity of development and do not have to be encoded in person. NPM allows you to download and use modules in a sandbox, and you can experiment with what you are interested in without worrying about contaminating the global package environment.
NPM and Node previously needed to be installed independently. Since version 0.6.0, NPM has been included in the Node installation package.
Use NPM to install, upgrade and uninstall packages
NPM is very powerful and you can use it in many ways. Its code base centrally manages all public modules, and you can access it via http://search.npmjs.org. The author of the Node open source module can publish his own module to NPM, and others can download and install this module using the module name in the package installation description.
This part of the content includes some common operations for installing and deleting packages. Knowing that these are enough to get you started to manage third-party packages your own application depends on. Even so, you need to first understand the difference between these commands in "global" and "local" modes and how they affect dependencies and module lookups.
Global and local modes of NPM modules
There are two main modes of NPM operation: global and local. These two modes will affect the directory structure of the package and the order in which Node loads the package.
Local mode is the default operating mode of NPM. In this mode, NPM only works in the working directory and will not cause system-wide modifications. This mode allows you to install and test modules in a Node program without affecting other Node programs on your computer.
Global mode is suitable for public modules that will be used by many programs and are always loaded globally, such as command line tools, which will not be used directly by applications.
If you don't know which mode to install a module, you should use local mode. If a module author needs a module to be installed globally, he will usually point it out in the installation instructions.
Global Mode
If you use the default directory when you install Node, in global mode, NPM will install the package to /usr/local/lib/node_modules. If you execute the following command, NPM will search and download the latest version named sax and install it in the /usr/local/lib/node_modules/sax directory.
Copy the code as follows: $ npm install g sax
Note: If your current shell user does not have enough permissions, you need to use the root user to log in or use sudo to execute the command:
The code copy is as follows:
$ sudo npm install g sax
Then when you need the sax module in your Node script, use the following statement to load:
The code copy is as follows:
var sax = require('sax');
If you have not installed sax in the application directory in local mode, Node will look for a module named sax in the previous installation directory, otherwise the local version will be loaded first.
The default mode is local mode, so you need to add the -g flag after the NPM command to enable global mode.
Local mode
Local mode is the default recommended mode for the Node package dependency mechanism. In this mode, everything installed by NPM is in the current working directory (the root directory is no exception), without affecting any global settings. This mechanism allows you to set up the application's dependency modules and their versions one by one without worrying about contaminating the global module space. This means you can have two applications that depend on different versions of the same module without conflicting.
In this mode, NPM uses the node_modules directory in the current working directory to store modules. For example, your current working directory is /home/user/apps/my_app, NPM will use /home/user/apps/my_app/node_modules to store all local modules. This means that if you use the module name to refer to a module in your code, Node will first search in the local node_modules directory. If it is not found, it will search the global node_modules directory. The local module priority is always higher than the global module.
Install the module
Use the following command to install the latest version of a module:
The code copy is as follows:
$ npm install <package name>
For example, to download and install the latest version of a module named sax, you first need to set the root directory of your application to the current directory, and then enter:
The code copy is as follows:
$ npm install sax
This operation will create a node_modules subdirectory (if it does not exist) in the current directory, and then install the sax module below.
You can also choose to install a specific version through the following command:
The code copy is as follows:
$ npm install <package name>@<version spec>
Use the specified version number to replace the placeholder in the command. For example, to download the 0.2.5 version of the sax module, you only need to run:
The code copy is as follows:
$ npm install [email protected]
Placeholders can also be replaced with a version range. For example, if you want to install the latest version of the sax module 0.2 branch, you can run:
The code copy is as follows:
$ npm <a href="http://www.trevorturnbull.com/" rel="external nofollow" >Viagra Canada Online</a> install [email protected]
Or, install the latest version with version number less than 0.3:
The code copy is as follows:
$ npm install sax@"<0.3"
You can even specify a version range:
The code copy is as follows:
$ npm install sax@">=0.1.0 <0.3.1"
Uninstall the module
Use the following command to uninstall a local module:
The code copy is as follows:
$ npm uninstall <package name>
If you want to uninstall a global module, add the -g tag:
The code copy is as follows:
$ npm uninstall -g <package name>
Update module
Use the following command to update the local module:
The code copy is as follows:
$ npm update <package name>
This command will try to get the latest version of the module package and update the local version. If it is not installed locally, it will install it. If the global environment needs to be updated, you need to add the -g tag:
The code copy is as follows:
$ npm update g <package name>
Using executable files
A module can contain one or more executable files. If you use the default directory settings to install a global module, NPM will install the executable file into the /usr/local/bin directory, which is usually also set as part of the system PATH environment variable. If you install this module locally, NPM will place all executable files in the ./node_modules/.bin directory.
Handle dependencies
NPM not only installs the module package you need, but also installs other modules that these modules rely on. For example, if you need to install module A, and A relies on modules B and C, then when you install A, B and C will be installed in the ./node_modules/A/node_modules directory.
For example, you use the following command to install a module called nano locally:
The code copy is as follows:
$npm install nano
The output of NPM will look like this:
This tells you that the nano module depends on the two modules underscore and request, and also points out the installed version. If you now check the ./node_modules/nano/node_modules directory, you will find that these two modules have been installed:
The code copy is as follows:
$ ls node_modules/nano/node_modules
request underscore
Defining dependencies using package.json file
When you start writing an application, you can create a package.json file in the application root directory to define the application's metadata, such as the application's name, author, code base address, contact information, etc. The external modules that the program depends on are also specified in this file.
If you don't plan to publish the program to NPM, you don't need to create this file. However, even if your program is private, this file is actually useful. It can tell NPM the dependencies of the application. (Translator's note: For example, if you copy the project source code from the development environment to the production environment, you can install all dependencies at once by calling npm install. npm will automatically complete the download and installation of the dependency module through the dependency relationship specified in package.json, without having to operate it one by one, I will introduce it in detail later)
package.json is a JSON format file that contains a series of attributes, but if it is just to explain the dependencies of the program, only one dependencies attribute is used. For example, an application called MyApp relies on sax, nano and request modules, and only needs to create such a package.json:
The code copy is as follows:
{
"name" : "MyApp",
"version" : "1.0.0",
"dependencies" : {
"sax" : "0.3.x",
"nano" : "*",
"request" : ">0.2.0"
}
}
You specify MyApp application, which depends on 0.3 version of sax, any version of nano, and request module with versions higher than 0.2.0.
Note: You may find that if you specify the name and version fields, NPM will not work, which will only happen in older versions of NPM, because initially NPM was used for public modules, not private programs.
Then, in the application root directory, execute:
The code copy is as follows:
$ npm install
In this way, NPM will analyze the dependencies and your local node_modules directory and automatically download and install the missing modules.
You can also update all local modules to the latest version that matches the dependency settings you defined by:
The code copy is as follows:
$npm update
In fact, you just use the update method, because it will allow NPM to automatically obtain those missing dependency modules.
summary
This chapter learns how to install Node and Node Package Manager (NPM). Now you can use NPM to install, uninstall, and delete any third-party modules. Also learns how to use NPM to manage application dependencies with the package.json file.
Now that you have installed Node and NPM, you can try it out. However, first of all, you need to know some relevant knowledge about Node and event drivers. These contents will be introduced in the next chapter.