在CentOS5.6上安裝Node.js
之前聽(tīng)說(shuō)過(guò)Node.js,只是知道它可以應(yīng)用于服務(wù)器端,但是對(duì)很多具體的東西并不了解。今天在QCon上聽(tīng)了袁鋒的分享《Node.js脫離了瀏覽器的Javascript》之后,頓時(shí)有了想立刻試一下的沖動(dòng)。
Node.js的安裝步驟算是比較簡(jiǎn)單,沒(méi)有太多的彎路,主要參考文檔:
Building and Installing Node.js
1. 安裝Python
根據(jù)參考文檔說(shuō)明,從源代碼編譯安裝Node.js需要python2.6或者以上,而通過(guò)yum install python.x86_64只能得到2.4.3,所以也要通過(guò)源碼編譯安裝python。下面是命令:
- # 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
 
    完成安裝后,執(zhí)行python命令,可以進(jìn)入python的命令行窗口。
2. 安裝Node.js
按照文檔上的說(shuō)明通過(guò)git checkout代碼一直不能成功,無(wú)論是使用git://github.com/joyent/node.git還是https://github.com/joyent/node.git。所以只能從github網(wǎng)上上下載后再編譯安裝,具體步驟如下:
- # 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
 
只用簡(jiǎn)單的幾步就完成了安裝。雖然看起來(lái)安裝都是成功的,但是實(shí)際如何就要求我們寫(xiě)一個(gè)程序來(lái)進(jìn)行驗(yàn)證一下。由于最近也在學(xué)習(xí)MongoDB,所以就寫(xiě)一個(gè)讀取MongoDB數(shù)據(jù)庫(kù):計(jì)算actionId為772的日志總數(shù)。
1. 使用安裝mongodb驅(qū)動(dòng)
- <pre name="code" class="plain"># 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 --mongodb:native> =
 - = 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
 
根據(jù)提示執(zhí)行:
- # cd node_modules/mongodb
 - # bash ./install.sh
 
注意:驅(qū)動(dòng)必須安裝在項(xiàng)目所在的目錄下,并不是安裝一次所有項(xiàng)目都可以使用。
2. 編寫(xiě)測(cè)試代碼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/');
 
啟動(dòng)服務(wù)器:
- # node mongo.js
 
在瀏覽器訪問(wèn)http://127.0.0.1:3000,可以看到如下輸出:

 
現(xiàn)在可以說(shuō)前面的安裝過(guò)程是正確,開(kāi)了個(gè)好頭。
參考文章:
Getting started with VMWare CloudFoundry, MongoDB and Node.js
Blog rolling with mongoDB, express and Node.js
原文鏈接:http://blog.csdn.net/mydeman/article/details/6895223
【編輯推薦】















 
 
 



 
 
 
 