Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Heroku deployment ready files for testing MongoDB conncetion and Mong…

…oHQ add-ons
  • Loading branch information...
commit 9a3230b15041bd0e4f4967ed57987fa340b4fb8d 1 parent 6f8d4ce
@azat-co authored
View
3  .gitignore
@@ -1 +1,2 @@
-.DS_Store
+.DS_Store
+node_modules
View
1  db/Procfile
@@ -0,0 +1 @@
+web: node app.js
View
29 db/app.js
@@ -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
50 db/collections.js
@@ -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
11 db/package.json
@@ -0,0 +1,11 @@
+{
+ "name": "node-example",
+ "version": "0.0.1",
+ "dependencies": {
+ "mongodb":"",
+ "url":""
+ },
+ "engines": {
+ "node": "0.6.x"
+ }
+}
View
8 readme.md
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.