Permalink
Browse files

Heroku deployment ready files for testing MongoDB conncetion and Mong…

…oHQ add-ons
  • Loading branch information...
1 parent 6f8d4ce commit 9a3230b15041bd0e4f4967ed57987fa340b4fb8d Azat Mardanov committed Dec 1, 2012
Showing with 97 additions and 5 deletions.
  1. +2 −1 .gitignore
  2. +1 −0 db/Procfile
  3. +29 −0 db/app.js
  4. +50 −0 db/collections.js
  5. +11 −0 db/package.json
  6. +4 −4 readme.md
View
@@ -1 +1,2 @@
-.DS_Store
+.DS_Store
+node_modules
View
@@ -0,0 +1 @@
+web: node app.js
View
@@ -0,0 +1,29 @@
+/*
+Rapid Prototyping with JS is a JavaScript and Node.js book that will teach you how to build mobile and web apps fast. — Read more at
+http://rapidprototypingwithjs.com.
+*/
+var util = require('util');
+var url = require('url');
+var http=require('http');
+var mongodb = require ('mongodb');
+var Db = mongodb.Db;
+var Connection = mongodb.Connection;
+var Server = mongodb.Server;
+var port = process.env.PORT || 1337;
+var dbConnUrl = process.env.MONGOHQ_URL || "mongodb://@127.0.0.1:27017";
+var dbHost = url.parse(dbConnUrl).hostname;
+var dbPort = new Number(url.parse(dbConnUrl).port);
+console.log(dbHost+dbPort)
+var db=new Db ('test', new Server(dbHost, dbPort, {}));
+db.open(function(e,c){
+ // console.log (util.inspect(db));
+ var server = http.createServer(function (req, res) { //creates server
+ res.writeHead(200, {'Content-Type': 'text/plain'}); //sets the right header and status code
+ res.end(db._state); //outputs string with line end symbol
+ });
+ server.listen(port, function() {
+ console.log('Server is running at %s:%s ', server.address().address, server.address().port); //sets port and IP address of the server
+ });
+ db.close();
+});
+
View
@@ -0,0 +1,50 @@
+/*
+Rapid Prototyping with JS is a JavaScript and Node.js book that will teach you how to build mobile and web apps fast. — Read more at
+http://rapidprototypingwithjs.com.
+*/
+var mongodb = require('mongodb');
+var url = require('url');
+var log = console.log;
+
+var connectionUri = url.parse(process.env.MONGOHQ_URL);
+var dbName = connectionUri.pathname.replace(/^\//, '');
+
+mongodb.Db.connect(process.env.MONGOHQ_URL, function(error, client) {
+ if (error) throw error;
+
+ client.collectionNames(function(error, names){
+ if(error) throw error;
+
+ //output all collection names
+ log("Collections");
+ log("===========");
+ var lastCollection = null;
+ names.forEach(function(colData){
+ var colName = colData.name.replace(dbName + ".", '')
+ log(colName);
+ lastCollection = colName;
+ });
+
+ var collection = new mongodb.Collection(client, lastCollection);
+ log("\nDocuments in " + lastCollection);
+ var documents = collection.find({}, {limit:5});
+
+ //output a count of all documents found
+ documents.count(function(error, count){
+ log(" " + count + " documents(s) found");
+ log("====================");
+
+ // output the first 5 documents
+ documents.toArray(function(error, docs) {
+ if(error) throw error;
+
+ docs.forEach(function(doc){
+ log(doc);
+ });
+
+ // close the connection
+ client.close();
+ });
+ });
+ });
+});
View
@@ -0,0 +1,11 @@
+{
+ "name": "node-example",
+ "version": "0.0.1",
+ "dependencies": {
+ "mongodb":"",
+ "url":""
+ },
+ "engines": {
+ "node": "0.6.x"
+ }
+}
View
@@ -32,14 +32,14 @@ Rapid Prototyping with JS is a JavaScript and Node.js book that will teach you h
[NodeJS] "Hello World" app, ready for deployment
+* [test](https://github.com/azat-co/rpjs/tree/master/test)
+
+ Message Board back-end: TDD with run-time storage
+
* [db](https://github.com/azat-co/rpjs/tree/master/db)
Simple app to test local database connection with MongoDB Native Driver
-* [test](https://github.com/azat-co/rpjs/tree/master/test)
-
- Message Board back-end: TDD with run-time storage
-
* [mongo](https://github.com/azat-co/rpjs/tree/master/mongo)
Message Board back-end: [MongoDB] storage, ready for deployment

0 comments on commit 9a3230b

Please sign in to comment.