In the last article, "Installing Node.js on CentOS5.6", the installation was completed 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.
1. Use installation MongoDB drive
# 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
====================================================== ==========================
= = = Install with C + + Bson parser do <npm install 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
Follow the prompts:
# 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.
2. Write test code Mongo.js
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 is ' + 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:
# node Mongo.js
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.
Reference articles:
Getting Started with VMWare Cloudfoundry, MongoDB and Node.js
Blog rolling with MongoDB, Express and Node.js