From a65ba01f5ae51c62a7de9b51e34818d5c910a2bf Mon Sep 17 00:00:00 2001 From: Chris Shoff Date: Wed, 16 Feb 2011 09:57:14 -0800 Subject: [PATCH 1/2] Added some rough mongodb support. Requires mongodb to be installed, as well as the node.js mongodb interface. --- app.js | 9 ++++---- js/database.js | 25 +++++++++++++++++++++ features.js => js/features.js | 5 +++-- sample_features.js => js/sample_features.js | 0 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 js/database.js rename features.js => js/features.js (95%) rename sample_features.js => js/sample_features.js (100%) diff --git a/app.js b/app.js index 7c45b49..a80d14c 100644 --- a/app.js +++ b/app.js @@ -1,9 +1,10 @@ var http = require('http'), url = require('url'), - Features = require('./features.js').Features, - emitter = new(require('events').EventEmitter); - -features = new Features(); + Features = require('./js/features.js').Features, + db = require('./js/database.js').db, + emitter = new(require('events').EventEmitter), + +features = new Features(db); var app = http.createServer(function (req, res) { features.execute(req, res, function(msg, success) { diff --git a/js/database.js b/js/database.js new file mode 100644 index 0000000..1ec81a5 --- /dev/null +++ b/js/database.js @@ -0,0 +1,25 @@ +var sys = require('sys'), + Db = require('mongodb').Db, + Connection = require('mongodb').Connection, + Server = require('mongodb').Server, + BSON = require('mongodb').BSONNative; + + +var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost'; +var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT; + +var db = new Db('dynamic-node-server-mongo', new Server(host, port, {}), {native_parser:true}); + + +// Just a simple test to verify it's working. +db.open(function(err, db) { + db.collection('features', function(err, collection) { + collection.insert({'a':1}); + console.log('sample record added to db'); + collection.remove(function(err, collection) { + console.log('sample record removed from db'); + }); + }); +}); + +exports.db = db; diff --git a/features.js b/js/features.js similarity index 95% rename from features.js rename to js/features.js index 7b7ae71..677c85c 100644 --- a/features.js +++ b/js/features.js @@ -1,8 +1,9 @@ var url = require("url"), sample_features = require("./sample_features.js"); -function Features() { - this.feature_list = sample_features.get_sample_feature_list(); +function Features(db) { + this.db = db; + this.feature_list = sample_features.get_sample_feature_list(this.db); this.parse_url = function(url_string, parse_qs) { return url.parse(url_string, parse_qs); } this.execute = function(req, res, callback) { diff --git a/sample_features.js b/js/sample_features.js similarity index 100% rename from sample_features.js rename to js/sample_features.js From bc9f44f659ed7dd3d7a8c02f9ff0f39e84c1a849 Mon Sep 17 00:00:00 2001 From: Chris Shoff Date: Wed, 16 Feb 2011 10:02:19 -0800 Subject: [PATCH 2/2] Update README. --- README | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README b/README index d0b6612..e918890 100644 --- a/README +++ b/README @@ -2,6 +2,12 @@ The purpose of this project is to create a dynamic node.js server that can be ex For now, it's only a quick skeleton with some sample features. +Requires mongodb to be installed - can be installed via your package manager or from http://www.mongodb.org + +Also requires the mongodb native node.js driver - can be installed with npm using "npm install mongodb" or cloned from https://github.com/christkv/node-mongodb-native + +These dependencies may be added to the project at some point. Live with it for now. + Run with: node app.js