summaryrefslogtreecommitdiffstats
path: root/examples/nodejs-mongodb-mongoose-restify
diff options
context:
space:
mode:
Diffstat (limited to 'examples/nodejs-mongodb-mongoose-restify')
-rw-r--r--examples/nodejs-mongodb-mongoose-restify/.npmignore1
-rw-r--r--examples/nodejs-mongodb-mongoose-restify/README.md34
-rw-r--r--examples/nodejs-mongodb-mongoose-restify/app/index.html4
-rw-r--r--examples/nodejs-mongodb-mongoose-restify/server-http.js4
-rw-r--r--examples/nodejs-mongodb-mongoose-restify/server-mongo/README.md5
-rw-r--r--examples/nodejs-mongodb-mongoose-restify/server-mongo/server-mongo.js2
-rw-r--r--examples/nodejs-mongodb-mongoose-restify/server.js57
7 files changed, 76 insertions, 31 deletions
diff --git a/examples/nodejs-mongodb-mongoose-restify/.npmignore b/examples/nodejs-mongodb-mongoose-restify/.npmignore
new file mode 100644
index 0000000..81db551
--- /dev/null
+++ b/examples/nodejs-mongodb-mongoose-restify/.npmignore
@@ -0,0 +1 @@
+!./config.js \ No newline at end of file
diff --git a/examples/nodejs-mongodb-mongoose-restify/README.md b/examples/nodejs-mongodb-mongoose-restify/README.md
index 16f74cc..4b0a51b 100644
--- a/examples/nodejs-mongodb-mongoose-restify/README.md
+++ b/examples/nodejs-mongodb-mongoose-restify/README.md
@@ -1,20 +1,37 @@
# Simple example - Node.js, Restify, MongoDb and Mongoose
-original author: Thomas Davis | https://github.com/thomasdavis
-editor/hi-jacker: Brandon Flowers | https://github.com/headwinds
+authors: Thomas Davis | https://github.com/thomasdavis
+ Brandon Flowers | https://github.com/headwinds
-There are basically two big parts to this demo - two servers. The first server, httpServer, serves up static html/js/css to the
-browser and the second, mongodbServer, is purely for saving and retrieving data from the mongodb.
+If you would like to discuss any of this code, please your leave comments using disqus at the bottom of this article:
+
+http://backbonetutorials.com/nodejs-restify-mongodb-mongoose/
+
+## STRUCTURE
+
+There are basically two parts to this demo - two servers - within one file, server.js, which may sound a little confusing but the two servers do different things.
+
+The first server, httpServer, serves up static html/js/css to the browser and the second, mongodbServer, is purely for saving and retrieving data from the mongodb.
I've put both servers in the server.js which makes it extremely long and challenging to maintain but it does the job for this demo. Also, each server is listening on its own port. If you are only allowed access to one public port, you could choose one server to listen on that port then pass the events to the other server which may be interested in different routes. For instance, in this case, if the http server listens for a /messages route, it could trigger an event and pass that to the mongo server.
-In addition to server.js, I've also refactored it into two separate files: server-http.js and server-mongo.js.
+In addition to server.js, I've also refactored it into two separate files: server-http.js and server-mongo.js.
+
+If you only need the mongobd server, you might start with the server-mongo folder.
## HTTP SERVER
-Originally, this tutorial started out as purely a mongodb one but I wanted to see the data in a browser and since this is a collection of Backbone tutorials, I might as well include some client-side backbone views. I aslo started working on it before discovering Google's Yeoman which includes its own web server that serves static files thus making the HTTP portion not necessary when testing locally, however, when you move to host these files somewhere else like nodejitsu, you may need to use your own static web server if it doesn't support nginx or apache.
+Originally, this tutorial started out as purely a mongodb one but I wanted to see the data in a browser and since this is a collection of Backbone tutorials, I might as well include some client-side backbone views. I aslo started working on it before discovering Google's Yeoman which includes its own web server that serves static files thus making the HTTP portion not necessary when testing locally, however, when you move to host these files somewhere else like nodejitsu, you may need to use your own static web server if it doesn't support nginx or apache.
+
+But before using Yeoman, you might want to try open a terminal to the directory of this app and typing:
-To view the data in your browser, you will need to host the app locally. In my case, I started up Yeoman using the terminal.
+$ node server
+
+Next, you will need to open browser and point it to:
+
+http://localhost:8080/
+
+Alternatively, you can use Yeoman which would automatically launch a browswer window and also you more features like live reload.
$ yeoman server
@@ -24,9 +41,8 @@ Yeoman automatically launches a browser window to:
http://localhost:3501/
-You can also see this same view by visiting the redundant http server:
+http server:
-http://localhost:8080/
If you'd like to see the raw messages as a json dump, you can point your browser to:
diff --git a/examples/nodejs-mongodb-mongoose-restify/app/index.html b/examples/nodejs-mongodb-mongoose-restify/app/index.html
index bbcd3e5..75334e6 100644
--- a/examples/nodejs-mongodb-mongoose-restify/app/index.html
+++ b/examples/nodejs-mongodb-mongoose-restify/app/index.html
@@ -23,10 +23,9 @@
</p>
<p>Check list</p>
<ol>
- <li>Have you installed and started mongod in your terminal window? $ mongod</li>
+ <li>Have you installed and started mongodb in your terminal window? type: mongod</li>
<li>Have you opened a second terminal window and created a sample message in the messages collection of your local mongodb database?</li>
<li>After creating a sample message, you should start your server.js by typing: node server</li>
- <li>Finally, open yet another terminal window and type: yeoman server</li>
</ol>
<h2>Messages</h2>
</div>
@@ -36,6 +35,7 @@
<div class="resources">
<p>
If you start to see your messages build up in the Message box above, congratulations! You've successfully setup this simple demo.
+ </p>
</div>
</body>
diff --git a/examples/nodejs-mongodb-mongoose-restify/server-http.js b/examples/nodejs-mongodb-mongoose-restify/server-http.js
index 223c250..08c5ff8 100644
--- a/examples/nodejs-mongodb-mongoose-restify/server-http.js
+++ b/examples/nodejs-mongodb-mongoose-restify/server-http.js
@@ -47,9 +47,9 @@ var getFilePath = function(url) {
console.log("url: " + url);
- var filePath = './public' + url;
+ var filePath = './app' + url;
- if (url == '/' ) filePath = './public/index.html';
+ if (url == '/' ) filePath = './app/index.html';
console.log("filePath: " + filePath);
diff --git a/examples/nodejs-mongodb-mongoose-restify/server-mongo/README.md b/examples/nodejs-mongodb-mongoose-restify/server-mongo/README.md
index 12eee80..6aeaeed 100644
--- a/examples/nodejs-mongodb-mongoose-restify/server-mongo/README.md
+++ b/examples/nodejs-mongodb-mongoose-restify/server-mongo/README.md
@@ -2,11 +2,6 @@
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:
diff --git a/examples/nodejs-mongodb-mongoose-restify/server-mongo/server-mongo.js b/examples/nodejs-mongodb-mongoose-restify/server-mongo/server-mongo.js
index 9fbbf28..9a5db33 100644
--- a/examples/nodejs-mongodb-mongoose-restify/server-mongo/server-mongo.js
+++ b/examples/nodejs-mongodb-mongoose-restify/server-mongo/server-mongo.js
@@ -84,7 +84,7 @@ 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 open your browser to http://localhost:8888/messages \n\n';
consoleMessage += '+++++++++++++++++++++++++++++++++++++++++++++++++++++ \n\n'
console.log(consoleMessage, mongodbServer.name, mongodbServer.url);
diff --git a/examples/nodejs-mongodb-mongoose-restify/server.js b/examples/nodejs-mongodb-mongoose-restify/server.js
index 7627413..3713bad 100644
--- a/examples/nodejs-mongodb-mongoose-restify/server.js
+++ b/examples/nodejs-mongodb-mongoose-restify/server.js
@@ -50,8 +50,8 @@ var sendHTML = function( filePath, contentType, response ){
var getFilePath = function(url) {
- var filePath = './public' + url;
- if (url == '/' ) filePath = './public/index.html';
+ var filePath = './app' + url;
+ if (url == '/' ) filePath = './app/index.html';
console.log("url: " + url)
@@ -135,28 +135,59 @@ var MessageSchema = new Schema({
// Use the schema to register a model
mongoose.model('Message', MessageSchema);
-var Message = mongoose.model('Message');
+var MessageMongooseModel = mongoose.model('Message'); // just to emphasize this isn't a Backbone Model
+
+
+/*
+
+this approach was recommended to remove the CORS restrictions instead of adding them to each request
+but its not working right now?! Something is wrong with adding it to mongodbServer
+
+// Enable CORS
+mongodbServer.all( '/*', function( req, res, next ) {
+ res.header( 'Access-Control-Allow-Origin', '*' );
+ res.header( 'Access-Control-Allow-Method', 'POST, GET, PUT, DELETE, OPTIONS' );
+ res.header( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, X-File-Name, Content-Type, Cache-Control' );
+ if( 'OPTIONS' == req.method ) {
+ res.send( 203, 'OK' );
+ }
+ next();
+});
+
+
+*/
// 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");
+ res.header( 'Access-Control-Allow-Origin', '*' );
+ res.header( 'Access-Control-Allow-Method', 'POST, GET, PUT, DELETE, OPTIONS' );
+ res.header( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, X-File-Name, Content-Type, Cache-Control' );
+
+ if( 'OPTIONS' == req.method ) {
+ res.send( 203, 'OK' );
+ }
console.log("mongodbServer getMessages");
- Message.find().limit(20).sort('date', -1).execFind(function (arr,data) {
+ MessageMongooseModel.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");
+ res.header( 'Access-Control-Allow-Origin', '*' );
+ res.header( 'Access-Control-Allow-Method', 'POST, GET, PUT, DELETE, OPTIONS' );
+ res.header( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, X-File-Name, Content-Type, Cache-Control' );
+
+ if( 'OPTIONS' == req.method ) {
+ res.send( 203, 'OK' );
+ }
+
// Create a new message model, fill it up and save it to Mongodb
- var message = new Message();
+ var message = new MessageMongooseModel();
console.log("mongodbServer postMessage: " + req.params.message);
@@ -172,9 +203,9 @@ mongodbServer.listen(mongodbPort, function() {
var consoleMessage = '\n A Simple MongoDb, Mongoose, Restify, and Backbone Tutorial'
consoleMessage += '\n +++++++++++++++++++++++++++++++++++++++++++++++++++++'
consoleMessage += '\n\n %s says your mongodbServer is listening at %s';
- consoleMessage += '\n great! now open your browser to http://localhost:3501';
- consoleMessage += '\n where you will connect to your httpServer that will';
- consoleMessage += '\n talk to your mongodbServer to get and post your messages. \n\n';
+ consoleMessage += '\n great! now open your browser to http://localhost:8080';
+ consoleMessage += '\n it will connect to your httpServer to get your static files';
+ consoleMessage += '\n and talk to your mongodbServer to get and post your messages. \n\n';
consoleMessage += '+++++++++++++++++++++++++++++++++++++++++++++++++++++ \n\n'
console.log(consoleMessage, mongodbServer.name, mongodbServer.url);
@@ -184,3 +215,5 @@ mongodbServer.listen(mongodbPort, function() {
mongodbServer.get('/messages', getMessages);
mongodbServer.post('/messages', postMessage);
+
+