CentOS上安装Node.js和mongodb笔记

2020-06-17 06:47:32易采站长站整理

=  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

根据提示执行:

# 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,可以看到如下输出:

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