Node. js is a set of JavaScript toolkit used to write high-performance network servers. A series of changes started from this. Specifically, Node. js assumes that you are running it in a POSIX environment (Linux or MacOSX ). If you are running Windows, install MinGW to obtain a POSIX-like environment. In Node, Http is the first priority. Node is optimized to create an http server, so most of the examples and libraries you see on the Internet are concentrated on the web (
Node. js is a set of JavaScript toolkit used to write high-performance network servers. A series of changes started from this. Specifically, Node. js assumes that you are running it in a POSIX environment (Linux or Mac OS X ). If you are running Windows, install MinGW to obtain a POSIX-like environment. In Node, Http is the first priority. Node is optimized to create an http server, so most examples and libraries you can see on the Internet are concentrated on the web (http framework, template library, etc ).
Step 1: Install the dependency package
1. Install python version 2.6 or later (Ubuntu has been installed by default. You can use the pyhton-v command in terminal to view the python version ).
2. install other dependency packages:
Sudo apt-get install g ++ curl libssl-dev apache2-utils
3. Install the git tool:
Sudo apt-get install git-core
Step 2: Obtain the source code
Git clone git: // github.com/joyent/node.git
Wait until the download is completed. The size is 20 + m.
Step 3: Specify the compilation version, which is important!
1. Go to the folder where the source code is downloaded: Cd node
2. Specify the migrated version: Git checkout v0.4.10
This is very important because the latest node version is 0.5.5-pre, but many commonly used packages only support 0.4.10 (such as express). If you use the latest node version, npm cannot download the corresponding package.
3. Specify the path for compilation and execution:
- Mkdir ~ /Local
- ./Configure-prefix = $ HOME/local/node
- Make
- Make install
- Echo 'export PATH = $ HOME/local/node/bin: $ path'> ~ /. Profile
- Echo 'export NODE_PATH = $ HOME/local/node: $ HOME/local/node/lib/node_modules '> ~ /. Profile
- Source ~ /. Profile
Step 4: Set the environment variable. If you want to continue using the node command after restart, you need to set the environment variable:
Run sudo gedit/etc/profile to open the configuration file. Add the following two lines at the end of the file:
Export PATH = "$ HOME/local/node/bin: $ PATH"
Export NODE_PATH = "$ HOME/local/node: $ HOME/local/node/lib/node_modules"
Save and restart the system to make the settings take effect.
Step 5: Install npm Curl http://npmjs.org/install.sh | sh
Install the corresponding package as needed, such as express:
Npm install express
If the command does not respond for a long time after it is entered, you can view the detailed execution information by adding the-verbose parameter, that is:
Npm install express-verbose
Generally, downloading fails for two reasons:
1. The network speed is too slow and exits upon timeout.
2. the node version is too new, and the downloaded package is not supported. (The solution is described in step 3 .)
Step 6: test the HelloWorld program to save the following code as a hello. js file.
- VarHttp = require ('Http');
- Http. createServer (Function(Req, res ){
- Res. writeHead (200 ,{'Content-type':'Text/plain'});
- Res. end ('Hello Node. jsn');
- }). Listen (8124,"127.0.0.1");
- Console. log ('Server running at http: // 127.0.0.1: 8124 /');
Then run the following command: Node hello. jsStart the service.
Open your browser and enter the address: http: // 127.0.0.1: 8124/
If Hello Node. jsn is returned, the installation is successful!