From 4ad8bb473eed3cd3234735cf6be62a6d772313de Mon Sep 17 00:00:00 2001 From: Ben Wen Date: Mon, 19 Nov 2012 23:29:44 -0800 Subject: [PATCH] Housecleaning. --- diff.txt | 167 ------------------------------------------------------- 1 file changed, 167 deletions(-) delete mode 100644 diff.txt diff --git a/diff.txt b/diff.txt deleted file mode 100644 index d2c5e45..0000000 --- a/diff.txt +++ /dev/null @@ -1,167 +0,0 @@ -diff --git a/.gitignore b/.gitignore -index ce4b751..4ba468b 100644 ---- a/.gitignore -+++ b/.gitignore -@@ -7,7 +7,7 @@ lib-cov - *.pid - *.gz - --.DS_Stor -+.DS_Store - - pids - logs -diff --git a/README.md b/README.md -index 8b9abc0..16b172c 100644 ---- a/README.md -+++ b/README.md -@@ -1,8 +1,8 @@ --* hello-mongoose: Mongoose.js: elegant MongoDB object models for Node.js. -+# hello-mongoose: Mongoose.js: elegant MongoDB object models for Node.js. - ========================= - - MongoDB and Node.js are often used together because of their shared use of Javascript and its Object Notation (JSON). Mongoose is a popular helper library that provides a more rigorous modeling environment for your data, enforcing a little bit more structure as needed, while still maintaining flexibility that makes MongoDB powerful. In this article, you make a connection to a hosted MongoDB instance at add-on provider MongoLab with Mongoose and model a simple object. - --** License -+## License - - MIT Licensed -diff --git a/app.js b/app.js -index aabc11c..6e28c46 100644 ---- a/app.js -+++ b/app.js -@@ -40,13 +40,18 @@ var mongoose = require ("mongoose"); // The reason for this demo. - // Here we find an appropriate database to connect to, defaulting to - // localhost if we don't find one. - var uristring = -+ process.env.MONGODB_URI || - process.env.MONGOLAB_URI || - process.env.MONGOHQ_URL || - 'mongodb://localhost/HelloMongoose'; - - // The http server will listen to an appropriate port, or default to --// port 5000. -+// port 2000. - var theport = process.env.PORT || 5000; -+ -+// MongoDB's default is for speed over safety on writes. Most -+// applications should prefer safety over speed, so that's what -+// we use below. - var mongoOptions = { db: { safe: true }}; - - // Makes connection asynchronously. Mongoose will queue up database -@@ -63,47 +68,25 @@ mongoose.connect(uristring, mongoOptions, function (err, res) { - // statements. They enforce useful constraints on the data. - var userSchema = new mongoose.Schema({ - name: { -- first: String, -- last: { type: String, trim: true } -+ last: String, -+ first: { type: String, trim: true } - }, - age: { type: Number, min: 0} - }); - --// Compiles the schema into a model, opening (or creating, if -+// Instantiating an instance of the model, opening (or creating, if - // nonexistent) the 'PowerUsers' collection in the MongoDB database - var PUser = mongoose.model('PowerUsers', userSchema); - --// Clear out old data --PUser.remove({}, function(err) { -- if (err) { -- console.log ('error deleting old data.'); -- } --}); -- - // Creating one user. - var johndoe = new PUser ({ -- name: { first: 'John', last: ' Doe ' }, -- age: 25 -+ name: { last: 'John', first: ' Doe ' }, -+ age: Math.floor(Math.random()*91+1) - }); - - // Saving it to the database. If you restart and run this code - // multiple times, you will see multiple documents with random ages. --johndoe.save(function (err) {if (err) console.log ('Error on save!')}); -- --// Creating more users manually --var janedoe = new PUser ({ -- name: { first: 'Jane', last: 'Doe' }, -- age: 65 --}); --janedoe.save(function (err) {if (err) console.log ('Error on save!')}); -- --// Creating more users manually --var alicesmith = new PUser ({ -- name: { first: 'Alice', last: 'Smith' }, -- age: 45 --}); --alicesmith.save(function (err) {if (err) console.log ('Error on save!')}); -- -+johndoe.save(); - - // In case the browser connects before the database is connected, the - // user will see this message. -@@ -115,35 +98,23 @@ var found = ['DB Connection not yet established. Try again later. Check the co - // As new http requests arrive, the callback function gets invoked. - http.createServer(function (req, res) { - res.writeHead(200, {'Content-Type': 'text/html'}); -- createWebpage(req, res); --}).listen(theport); -- --function createWebpage (req, res) { -- // Let's find all the documents -+ // Lets find all the documents - PUser.find({}).exec(function(err, result) { - if (!err) { -- res.write(html1 + JSON.stringify(result, undefined, 2) + html2 + result.length + html3); -- // Let's see if there are any senior citizens (older than 64) with the last name Doe using the query constructor -- var query = PUser.find({'name.last': 'Doe'}); // (ok in this example, it's all entries) -- query.where('age').gt(64); -- query.exec(function(err, result) { -- if (!err) { -- res.end(html4 + JSON.stringify(result, undefined, 2) + html5 + result.length + html6); -- } else { -- res.end('Error in second query. ' + err) -- } -- }); -+ res.end(html1 + result.length + html2 + JSON.stringify(result, undefined, 2) + html3); - } else { -- res.end('Error in first query. ' + err) -+ res.end('Error in query. ' + err) - }; - }); --} -+}).listen(theport); - - // Tell the console we're getting ready. - // The listener in http.createServer should still be active after these messages are emitted. - console.log('http server will be listening on port %d', theport); - console.log('CTRL+C to exit'); - -+// DONE. -+ - // - // House keeping. - -@@ -153,15 +124,10 @@ var html1 = ' hello-mongoose: MongoLab MongoDB Mongoose Node.js Demo on H - <head> \ - <style> body {color: #394a5f; font-family: sans-serif} </style> \ - </head> \ --<body> \ - <h1> hello-mongoose: MongoLab MongoDB Mongoose Node.js Demo on Heroku </h1> \ - <br\> \ --<br\> <h2> All Documents in MonogoDB database </h2> <pre><code> '; --var html2 = '</code></pre> <br\> <i>'; --var html3 = ' documents. </i> <br\> <br\>'; --var html4 = '<h2> Queried (name.last = "Doe", age >64) Documents in MonogoDB database </h2> <pre><code> '; --var html5 = '</code></pre> <br\> <i>'; --var html6 = ' documents. </i> <br\> <br\> \ --<br\> <br\> <center><i> Demo code available at <a href="http://github.com/mongolab/hello-mongoose">github.com</a> </i></center>'; -+There are: '; -+var html2 = ' documents. <br\> <br\> <h2> Documents in MonogoDB database </h2> <pre><code>'; -+var html3 = '</code></pre> <br\> <br\> <center><i> Demo code available at <a href="http://github.com/mongolab/hello-mongoose">github.com</a> </i></center>'; - -