Home >Web Front-end >JS Tutorial >Installing Node.js and mongodb notes on CentOS_node.js

Installing Node.js and mongodb notes on CentOS_node.js

WBOY
WBOYOriginal
2016-05-16 16:02:201362browse

I have heard of Node.js before. I just know that it can be applied to the server side, but I don’t know many specific things. After listening to Yuan Feng's sharing "Node.js breaks away from browser Javascript" at QCon today, I suddenly had the urge to try it out immediately.

The installation steps of Node.js are relatively simple without too many detours. The main reference document is:

Building and Installing Node.js

1. Install Python

According to the reference documentation, compiling and installing Node.js from source code requires python2.6 or above, but you can only get 2.4.3 through yum install python.x86_64, so you must also compile and install python through source code. Here is the command:

Copy code 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 completing the installation, execute the python command to enter the python command line window.

2. Install Node.js

According to the instructions in the document, git checkout code has never been successful, whether using git://github.com/joyent/node.git or 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:

Copy code 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 node
# ln -s /opt/node/bin/node-waf node-waf

3. Install npm

npm is a package manager used to install the node.js library. The installation command is quite simple:

Copy code The code is as follows:

At this point, 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, the actual situation requires us to write a program to verify it. Since I am also learning MongoDB recently, I wrote a method to read the MongoDB database: calculate the total number of logs with actionId 772.

4. Install mongodb driver using

Copy code The code is 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    = 
=  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

根据提示执行:

复制代码 代码如下:

# cd node_modules/mongodb 
# bash ./install.sh 

注意:驱动必须安装在项目所在的目录下,并不是安装一次所有项目都可以使用。

5. 编写测试代码mongo.js

复制代码 代码如下:

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({'action': 772}, function(err, count){ 
        res.write('The total of action 772 is ' count ".n"); 
        res.end(); 
      }); 
    }); 
  }); 
}).listen(3000, '127.0.0.1'); 
 
console.log('Server running at http://127.0.0.1:3000/'); 

启动服务器:
复制代码 代码如下:

# node mongo.js 

在浏览器访问http://127.0.0.1:3000,可以看到如下输出:

现在可以说前面的安装过程是正确,开了个好头。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn