This article describes how to install Node on CentOS. js and mongodb notes, this article explains Python installation, Node. js installation, npm installation, mongodb driver installation, mongodb database operation test code, and other content. if you need it, you can refer to the Node. js, but knows that it can be applied to the server side, but does not know much about a lot of specific things. Today, I listened to Yuan Feng's sharing "Node. js is out of the browser's Javascript" on QCon, and I suddenly got the urge to try it out immediately.
The installation steps of Node. js are relatively simple and there are not many detours. refer to the following documents:
Building and Installing Node. js
1. install Python
According to the instructions in the reference documents, compiling and installing Node. js from the source code requires python2.6 or later, and only 2.4.3 can be obtained through yum install python. x86_64, so python should also be compiled and installed through the source code. The following is a command:
The code is 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 installation, run the python command to enter the python command line window.
2. install Node. js
According to the instructions in the document, the git checkout code has never been successful. Whether you use git: // github.com/joyent/node.git, it is also https://github.com/joyent/node.git. Therefore, you can only download it from github and then compile and install it. the specific steps are as follows:
The code is 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
# Ln-s/opt/node/bin/node-waf
3. install npm
Npm is the package manager used to install the node. js library. the installation command is quite simple:
The code is as follows:
# Curl http://npmjs.org/install.sh | sh
So far, the installation is complete.
In the above content, the installation is completed in just a few simple steps. Although it seems that the installation is successful, we need to write a program for verification. Since I have been studying MongoDB recently, I wrote a log to read the MongoDB database: calculate the total number of logs with an actionId of 772.
4. install the mongodb driver
The code is as follows:
# Npm install mongodb
Npm WARN mongodb@0.9.6-23 package. json: bugs ['web'] shocould probably be bugs ['URL']
Npm WARN nodeunit@0.5.1 package. json: bugs ['web'] shocould 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 =
= 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
Execute as prompted:
The code is as follows:
# Cd node_modules/mongodb
# Bash./install. sh
Note: The driver must be installed in the directory where the project is located. it is not available for all projects once it is installed.
5. write the test code mongo. js.
The code is as follows:
Var http = require ('http ');
Var mongodb = require ('mongodb ');
Http. createServer (function (req, res ){
Res. writeHead (200, {'content-type': 'Text/plain; charset = utf-8 '});
Mongodb. connect ('mongodb: /localhost: 40202/log', function (err, conn ){
Conn. collection ('log', function (err, coll ){
Coll. count ({'actions': 772}, function (err, count ){
Res. write ('the total of action 772 is '+ count + ". \ n ");
Res. end ();
});
});
});
}). Listen (3000, '192. 0.0.1 ');
Console. log ('server running at http: // 127.0.0.1: 3000 /');
Start the server:
The code is as follows:
# Node mongo. js
Access http: // 127.0.0.1: 3000 in the browser and you can see the following output:
Now we can say that the previous installation process is correct and a good start is enabled.