I've heard of Node.js before, but I know it can be applied to the server, but I don't know a lot of specific things. Today in Qcon listened to Shong share "node.js out of the browser JavaScript," Immediately after the impulse to try immediately.
Node.js installation steps are relatively simple, not too many detours, the main reference documents:
Building and installing Node.js
1. Install Python
According to the reference documentation, compiling the installation node.js from the source code requires python2.6 or more, while the Yum install python.x86_64 can only get 2.4.3, so you also need to compile and install Python via source. Here's the command:
Copy Code code as follows:
# wget Http://www.python.org/ftp/python/3.2.2/Python-3.2.2.tgz
# tar XZVF python-3.2.3.tgz
# CD Python-3.2.2
#./configure
# make
# Make Test
# make Install
After the installation is complete, execute the python command and go to the Python Command line window.
2. Install Node.js
Follow the instructions on the document to never succeed with git checkout code, whether using Git://github.com/joyent/node.git or https://github.com/joyent/node.git. So you can only download and then compile the installation from the GitHub network, the following steps are specified:
Copy Code code as follows:
# wget Https://nodeload.github.com/joyent/node/tarball/master
# MV Master node.tar.gz
# tar XZVF node.tar.gz
# CD Joyent-node-84d0b1b
#./configure--prefix=/opt/node/
# make
# make Install
# Cd/usr/bin
# Ln-s/opt/node/bin/node Node
# ln-s/opt/node/bin/node-waf Node-waf
3. Install NPM
NPM is the package manager used to install the Node.js library, and the installation commands are fairly straightforward:
Copy Code code as follows:
# Curl Http://npmjs.org/install.sh | Sh
At this point, the installation is complete.
In the above section, the installation is done in a few simple steps. Although it looks like the installation was successful, it actually requires us to write a program to verify it. Since I've been learning MongoDB recently, I'm writing a read MongoDB database: Calculates the total number of logs that ActionId is 772.
4. Use Installation MongoDB Drive
Copy Code code as follows:
# NPM Install MongoDB
NPM WARN mongodb@0.9.6-23 package.json:bugs[' web ' should probably be bugs[' url '
NPM WARN nodeunit@0.5.1 package.json:bugs[' web ' should probably be bugs[' url '
> mongodb@0.9.6-23 Install/root/develop/node/node_modules/mongodb
> Bash./install.sh
================================================================================
= =
= To install with C + + Bson parser do <npm Install MongoDB =
= The parser only works for node 0.4.X or lower =
= =
================================================================================
Not building native library for Cygwin
Using GNU Make
Mongodb@0.9.6-23./node_modules/mongodb
Follow the prompts:
Copy Code code as follows:
# CD Node_modules/mongodb
# bash./install.sh
Note: The driver must be installed in the same directory as the project, not all items will be installed once.
5. Write test Code Mongo.js
Copy Code code as follows:
var http = require (' http ');
var MongoDB = require (' MongoDB ');
Http.createserver (function (req, res) {
Res.writehead ({' Content-type ': ' Text/plain;charset=utf-8 '});
Mongodb.connect (' Mongodb://localhost:40202/log ', function (err, conn) {
Conn.collection (' Log ', function (err, coll) {
Coll.count ({' Action ': 772}, Function (err, count) {
Res.write (' The total of action 772 ' + Count + '. \ n ');
Res.end ();
});
});
});
}). Listen (3000, ' 127.0.0.1 ');
Console.log (' Server running at Http://127.0.0.1:3000/');
To start the server:
Copy Code code as follows:
In the browser access http://127.0.0.1:3000, you can see the following output:
Now it can be said that the previous installation process is correct and open a good head.