summaryrefslogtreecommitdiffstats
path: root/examples/nodejs-mongodb-mongoose-restify/server-mongo
diff options
context:
space:
mode:
authorbrandon flowers <brandonflowers@gmail.com>2013-05-04 13:38:54 -0400
committerbrandon flowers <brandonflowers@gmail.com>2013-05-04 13:38:54 -0400
commit5986b9a64d1ebc19c2347593edd2cd33d634bace (patch)
treec883a10790b2a7a1df583cb09e1873a684a3763c /examples/nodejs-mongodb-mongoose-restify/server-mongo
parent404bea77ec9d8d23a94b4ee9e0d643b4871773d6 (diff)
downloadbackbonetutorials-5986b9a64d1ebc19c2347593edd2cd33d634bace.zip
backbonetutorials-5986b9a64d1ebc19c2347593edd2cd33d634bace.tar.gz
backbonetutorials-5986b9a64d1ebc19c2347593edd2cd33d634bace.tar.bz2
updates to restify example to add tests and Yeoman support
Diffstat (limited to 'examples/nodejs-mongodb-mongoose-restify/server-mongo')
-rw-r--r--examples/nodejs-mongodb-mongoose-restify/server-mongo/README.md38
-rw-r--r--examples/nodejs-mongodb-mongoose-restify/server-mongo/package.json17
-rw-r--r--examples/nodejs-mongodb-mongoose-restify/server-mongo/server-mongo.js96
3 files changed, 151 insertions, 0 deletions
diff --git a/examples/nodejs-mongodb-mongoose-restify/server-mongo/README.md b/examples/nodejs-mongodb-mongoose-restify/server-mongo/README.md
new file mode 100644
index 0000000..12eee80
--- /dev/null
+++ b/examples/nodejs-mongodb-mongoose-restify/server-mongo/README.md
@@ -0,0 +1,38 @@
+# Mongodb Server
+
+In order to persist data to a mongodb database, a server is created to handle all the CRUD operations.
+
+This demo attempts to answer two burning questions:
+
+1. how do I setup a local server to test mongodb?
+2. how do I deploy this server to the internet and use it to store date for my other projects?
+
+## LOCAL
+
+The quickest way to learn mongodb is by installing it on your local machine using a service like brew or mac ports. For instance, to install mongodb using brew open a terminal window:
+
+$ brew install mongo
+
+Once you have mongodb installed, you can start it up:
+
+$ mongod
+
+Then open another a terminal window to test it using the Mongo Shell:
+
+$ mongo
+
+For the shell, you can name and create your first database and collection. For this demo, I've created my database, "nationalpark"; added two collections: "hikers" and "notes"; and inserted a model into each collection:
+
+> use nationalpark
+> var hiker = { username: "headwinds", health: 100, mana: 100, backpack:[ "lighter", "solar charger", "wine gums" ] }
+> db.hikers.insert(hiker)
+> var note = { note: "Probably not a good idea to take this route with signs of rain but onwards, upwards!", username: "headwinds" }
+> db.notes.insert(note);
+> var hiker.insert
+
+## HOSTED
+
+The hosted version relys on the mongodb platform Mongohq (http://www.mongohq.com) which Nodjitsu (http://www.nodejitsu) offers for free to their subscribers but you should be able to use this approach for other platforms.
+
+The beauty of a hosted platform is that you can share your app with the world, and you don't have to worry about installing or scaling mongodb although you do need to pay for increased traffic and adding more apps.
+
diff --git a/examples/nodejs-mongodb-mongoose-restify/server-mongo/package.json b/examples/nodejs-mongodb-mongoose-restify/server-mongo/package.json
new file mode 100644
index 0000000..f14c171
--- /dev/null
+++ b/examples/nodejs-mongodb-mongoose-restify/server-mongo/package.json
@@ -0,0 +1,17 @@
+{
+ "name": "nationalpark-mongodb",
+ "subdomain": "nationalpark-mongodb",
+ "scripts": {
+ "start": "server-mongo.js"
+ },
+ "version": "0.0.0-14",
+ "engines": {
+ "node": "0.6.x"
+ },
+ "dependencies": {
+ "restify": "1.4.x",
+ "mongoose": "2.6.x",
+ "connect": "2.7.4",
+ "dtrace-provider":"0.2.8"
+ }
+}
diff --git a/examples/nodejs-mongodb-mongoose-restify/server-mongo/server-mongo.js b/examples/nodejs-mongodb-mongoose-restify/server-mongo/server-mongo.js
new file mode 100644
index 0000000..9fbbf28
--- /dev/null
+++ b/examples/nodejs-mongodb-mongoose-restify/server-mongo/server-mongo.js
@@ -0,0 +1,96 @@
+var mongoose = require('mongoose/');
+var restify = require('restify');
+
+var config = require('./config');
+
+var mongodbPort = process.env.PORT || 8888;
+
+/*
+
+ see README.md for a more detailed write up
+
+*/
+
+////////////////////////////////////////////////////// MONGODB - saves data in the database and posts data to the browser
+
+var mongoURI = ( process.env.PORT ) ? config.creds.mongoose_auth_mongohq : config.creds.mongoose_auth_local;
+
+db = mongoose.connect(mongoURI),
+Schema = mongoose.Schema;
+
+// require restify and bodyParser to read Backbone.js syncs
+var restify = require('restify');
+
+var mongodbServer = restify.createServer({
+ formatters: {
+ 'application/json': function(req, res, body){
+ if(req.params.callback){
+ var callbackFunctionName = req.params.callback.replace(/[^A-Za-z0-9_\.]/g, '');
+ return callbackFunctionName + "(" + JSON.stringify(body) + ");";
+ } else {
+ return JSON.stringify(body);
+ }
+ },
+ 'text/html': function(req, res, body){
+ return body;
+ }
+ }
+});
+
+mongodbServer.use(restify.bodyParser());
+
+// Create a schema for our data
+var MessageSchema = new Schema({
+ message: String,
+ date: Date
+});
+
+// Use the schema to register a model
+mongoose.model('Message', MessageSchema);
+var Message = mongoose.model('Message');
+
+
+// This function is responsible for returning all entries for the Message model
+var getMessages = function(req, res, next) {
+ // Resitify currently has a bug which doesn't allow you to set default headers
+ // This headers comply with CORS and allow us to mongodbServer our response to any origin
+ res.header("Access-Control-Allow-Origin", "*");
+ res.header("Access-Control-Allow-Headers", "X-Requested-With");
+
+ console.log("mongodbServer getMessages");
+
+ Message.find().limit(20).sort('date', -1).execFind(function (arr,data) {
+ res.send(data);
+ });
+}
+
+var postMessage = function(req, res, next) {
+ res.header("Access-Control-Allow-Origin", "*");
+ res.header("Access-Control-Allow-Headers", "X-Requested-With");
+ // Create a new message model, fill it up and save it to Mongodb
+ var message = new Message();
+
+ console.log("mongodbServer postMessage: " + req.params.message);
+
+ message.message = req.params.message;
+ message.date = new Date()
+ message.save(function () {
+ res.send(req.body);
+ });
+}
+
+mongodbServer.listen(mongodbPort, function() {
+
+ var consoleMessage = '\n MongoDb, Mongoose, Restify, and Backbone Tutorial'
+ consoleMessage += '\n +++++++++++++++++++++++++++++++++++++++++++++++++++++'
+ consoleMessage += '\n\n %s your mongodbServer is listening at %s';
+ consoleMessage += '\n\n open your browser to http://localhost:8888 \n\n';
+ consoleMessage += '+++++++++++++++++++++++++++++++++++++++++++++++++++++ \n\n'
+
+ console.log(consoleMessage, mongodbServer.name, mongodbServer.url);
+
+});
+
+mongodbServer.get('/messages', getMessages);
+mongodbServer.post('/messages', postMessage);
+