Permalink
Browse files

Fix db issues, db name is 'db'

  • Loading branch information...
1 parent 72a652d commit 4f737a7238899055e9c91891640ce5098d273df1 gatesvp committed Jun 15, 2011
Showing with 78 additions and 63 deletions.
  1. +7 −15 app.js
  2. +0 −2 app.js.1
  3. +31 −4 app.js.2
  4. +32 −26 app.js.3
  5. +8 −16 app.js.4
View
22 app.js
@@ -3,7 +3,6 @@ require.paths.unshift('./node_modules');
if(process.env.VCAP_SERVICES){
var env = JSON.parse(process.env.VCAP_SERVICES);
var mongo = env['mongodb-1.8'][0]['credentials'];
- mongo.db = "visits";
}
else{
var mongo = {
@@ -22,27 +21,21 @@ var generate_mongo_url = function(obj){
obj.db = (obj.db || 'test');
if(obj.username && obj.password){
- return "mongo://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db;
+ return "mongodb://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db;
}
else{
- return "mongo://" + obj.hostname + ":" + obj.port + "/" + obj.db;
+ return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db;
}
}
var mongourl = generate_mongo_url(mongo);
-/* Mongo Variables */
-var Db = require('mongodb').Db,
- Connection = require('mongodb').Connection,
- Server = require('mongodb').Server,
- BSON = require('mongodb').BSONNative;
-
/* Http Variables */
var port = (process.env.VMC_APP_PORT || 3000);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var http = require('http');
-var record_visit = function(req, res, db){
+var record_visit = function(req, res){
/* Connect to the DB and auth */
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('ips', function(err, coll){
@@ -61,11 +54,11 @@ var record_visit = function(req, res, db){
});
}
-var print_visits = function(req, res, db){
+var print_visits = function(req, res){
/* Connect to the DB and auth */
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('ips', function(err, coll){
- coll.find({}, {limit:5, sort:[['_id','desc']]}, function(err, cursor){
+ coll.find({}, {limit:10, sort:[['_id','desc']]}, function(err, cursor){
cursor.toArray(function(err, items){
res.writeHead(200, {'Content-Type': 'text/plain'});
for(i=0; i<items.length;i++){
@@ -80,13 +73,12 @@ var print_visits = function(req, res, db){
http.createServer(function (req, res) {
params = require('url').parse(req.url);
- var db = new Db('visits', new Server(mongo.hostname, mongo.port, {}), {} );
if(params.pathname === '/history') {
- print_visits(req, res, db);
+ print_visits(req, res);
}
else{
- record_visit(req, res, db);
+ record_visit(req, res);
}
}).listen(port, host);
View
@@ -1,5 +1,3 @@
-var env = JSON.parse(process.env.VCAP_SERVICES);
-
var port = (process.env.VMC_APP_PORT || 3000);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var http = require('http');
View
@@ -1,15 +1,42 @@
-var env = JSON.parse(process.env.VCAP_SERVICES);
+if(process.env.VCAP_SERVICES){
+ var env = JSON.parse(process.env.VCAP_SERVICES);
+ var mongo = env['mongodb-1.8'][0]['credentials'];
+}
+else{
+ var mongo = {
+ "hostname":"localhost",
+ "port":27017,
+ "username":"",
+ "password":"",
+ "name":"",
+ "db":"db"
+ }
+}
-/* Mongo Variables */
-var mongo = env['mongodb-1.8'][0]['credentials'];
+var generate_mongo_url = function(obj){
+ obj.hostname = (obj.hostname || 'localhost');
+ obj.port = (obj.port || 27017);
+ obj.db = (obj.db || 'test');
+
+ if(obj.username && obj.password){
+ return "mongodb://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db;
+ }
+ else{
+ return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db;
+ }
+}
+
+var mongourl = generate_mongo_url(mongo);
var port = (process.env.VMC_APP_PORT || 3000);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
- res.write(JSON.stringify(mongo));
+ res.write(mongourl+'\n');
+ res.write(JSON.stringify(mongo)+'\n\n');
+ res.write(JSON.stringify(process.env.VCAP_SERVICES)+'\n');
res.end('\n');
}).listen(port, host);
View
@@ -9,45 +9,51 @@ else{
"hostname":"localhost",
"port":27017,
"username":"",
- "password":"",
+ "password":"",
"name":"",
- "db":"db"
+ "db":""
}
}
-/* Mongo Variables */
-var Db = require('mongodb').Db,
- Connection = require('mongodb').Connection,
- Server = require('mongodb').Server,
- BSON = require('mongodb').BSONNative;
+var generate_mongo_url = function(obj){
+ obj.hostname = (obj.hostname || 'localhost');
+ obj.port = (obj.port || 27017);
+ obj.db = (obj.db || 'test');
-var db = new Db('visits', new Server(mongo.hostname, mongo.port, {}), {} );
+ if(obj.username && obj.password){
+ return "mongodb://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db + "?auto_reconnect=true";
+ }
+ else{
+ return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db;
+ }
+}
+
+var mongourl = generate_mongo_url(mongo);
/* Http Variables */
var port = (process.env.VMC_APP_PORT || 3000);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var http = require('http');
-/* Connect to the DB, auth and start web server */
-db.open(function(err, conn) {
- db.authenticate(mongo.username, mongo.password, function(err, success) {
- http.createServer(function (req, res) {
-
- /* Retrieve the 'ips' collection
- MongoDB will create the collection if needed. */
- conn.collection('ips', function(err, collection){
- /* Simple object to insert: ip address and date */
- ip = req.connection.remoteAddress;
- ts = new Date();
- object_to_insert = { 'ip': ip, 'ts': ts };
-
- /* Insert the object then print in response */
- /* Note the _id has been created */
- collection.insert( object_to_insert );
+var record_visit = function(req, res){
+ /* Connect to the DB and auth */
+ require('mongodb').connect(mongourl, function(err, conn){
+ conn.collection('ips', function(err, coll){
+ /* Simple object to insert: ip address and date */
+ object_to_insert = { 'ip': req.connection.remoteAddress, 'ts': new Date() };
+
+ /* Insert the object then print in response */
+ /* Note the _id has been created */
+ coll.insert( object_to_insert, {safe:true}, function(err){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(JSON.stringify(object_to_insert));
res.end('\n');
});
- }).listen(port, host);
+ });
});
-});
+}
+
+http.createServer(function (req, res) {
+ record_visit(req, res);
+}).listen(port, host);
+
View
@@ -3,7 +3,6 @@ require.paths.unshift('./node_modules');
if(process.env.VCAP_SERVICES){
var env = JSON.parse(process.env.VCAP_SERVICES);
var mongo = env['mongodb-1.8'][0]['credentials'];
- mongo.db = "visits";
}
else{
var mongo = {
@@ -12,7 +11,7 @@ else{
"username":"",
"password":"",
"name":"",
- "db":"visits"
+ "db":""
}
}
@@ -22,27 +21,21 @@ var generate_mongo_url = function(obj){
obj.db = (obj.db || 'test');
if(obj.username && obj.password){
- return "mongo://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db;
+ return "mongodb://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db;
}
else{
- return "mongo://" + obj.hostname + ":" + obj.port + "/" + obj.db;
+ return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db;
}
}
var mongourl = generate_mongo_url(mongo);
-/* Mongo Variables */
-var Db = require('mongodb').Db,
- Connection = require('mongodb').Connection,
- Server = require('mongodb').Server,
- BSON = require('mongodb').BSONNative;
-
/* Http Variables */
var port = (process.env.VMC_APP_PORT || 3000);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var http = require('http');
-var record_visit = function(req, res, db){
+var record_visit = function(req, res){
/* Connect to the DB and auth */
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('ips', function(err, coll){
@@ -61,11 +54,11 @@ var record_visit = function(req, res, db){
});
}
-var print_visits = function(req, res, db){
+var print_visits = function(req, res){
/* Connect to the DB and auth */
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('ips', function(err, coll){
- coll.find({}, {limit:5, sort:[['_id','desc']]}, function(err, cursor){
+ coll.find({}, {limit:10, sort:[['_id','desc']]}, function(err, cursor){
cursor.toArray(function(err, items){
res.writeHead(200, {'Content-Type': 'text/plain'});
for(i=0; i<items.length;i++){
@@ -80,13 +73,12 @@ var print_visits = function(req, res, db){
http.createServer(function (req, res) {
params = require('url').parse(req.url);
- var db = new Db('visits', new Server(mongo.hostname, mongo.port, {}), {} );
if(params.pathname === '/history') {
- print_visits(req, res, db);
+ print_visits(req, res);
}
else{
- record_visit(req, res, db);
+ record_visit(req, res);
}
}).listen(port, host);

0 comments on commit 4f737a7

Please sign in to comment.